I need some help removing values from an array.
This is the array
[
{
"label": "One",
"value": "one"
},
{
"label": "Two",
"value": "two"
},
{
"label": "Three",
"value": "three"
},
{
"label": "Four",
"value": "four"
},
{
"label": "Five",
"value": "five"
}
]
This is the code which I tried
useEffect(() => {
let od = [...options?.stockModeList]
let dc;
if (!isNaN(state?.parentVariant)){
dc = od.splice(options.length - 2, 2);
} else {
dc = od.splice(0, 3);
}
// and also I tried this as well
if (state?.parentVariant === null){
dc = od.splice(options.length - 2, 2);
} else {
dc = od.splice(0, 3);
}
// console.log(dc)
}, [ options?.stockModeList, state?.parentVariant])
The state?.parentVariant
variable is updating dynamically when the UI is changing it will mostly be a null value or integer value. So if its a null then I need to run the first statement and if it is an integer then I need to run the else statement.
But the issue is even after the value is been changed it always stays in the if statement.
2
Answers
I created a new array as stockModeOptions to save the updated array data. And I wrote 02 conditions to check whether its a null or whether it's an empty string.
And also there was a typo in the `od.splice(od.length -2,2); I was parsing the wrong data as well.
And also I am updating this useEffect each and everytime when the entire state updates as well