我需要一些帮助从数组中删除值。这是数组
[
{
"label": "One",
"value": "one"
},
{
"label": "Two",
"value": "two"
},
{
"label": "Three",
"value": "three"
},
{
"label": "Four",
"value": "four"
},
{
"label": "Five",
"value": "five"
}
]
这是我试过的代码
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])
当UI改变时,state?.parentVariant
变量会动态更新,它通常是一个空值或整数值。所以如果它是null,那么我需要运行第一个语句,如果它是一个整数,那么我需要运行else语句。
但问题是,即使在值被更改后,它也总是留在if语句中。
2条答案
按热度按时间cl25kdpy1#
我创建了一个新的数组作为stockModeOptions来保存更新的数组数据。我写了02个条件来检查它是null还是空字符串。
并且在“OD.splice(OD.长度-2,2)”中也有一个打印错误;我也分析了错误的数据。
而且我每次更新这个useEffect的时候,整个状态也会更新
svmlkihl2#