const prefer={"cs":{"c1":true,"c2":true,"c3":false,"c4":true}}
setArray((prevArray)=>{
return prevArray.filter(function(array){
//code here
})
});
我想在prefer对象的基础上设置数组,就像如果c1为true那么想将它添加到数组中 我还需要初始化数组吗? 在上面的示例中 array=["c1","c2","c4"] 。 我们还需要过滤方法还是可以使用其他方法
最佳答案
您可以使用 for-in 循环来迭代 map /对象
const prefer = { c1: true, c2: true, c3: false, c4: true };
let arr = [];
for (let key in prefer) {
if (prefer[key]) arr.push(key); // modify how u want to save
// arr.push({ [key]: prefer[key] }); // if u want key and value
}
console.log(arr);
关于javascript - 在对象的基础上添加数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60897975/