我有一个这样的数组:
const appoint =[
{ a: "asas",
b:{au: false, h:false,l:true}
},
{ a: "avd",
b:{au: true, h:false,l:true}
},
{ a: "as", b:{au: true, h:false,l:false}
}];
当我访问 b 时,我想过滤虚假值,到目前为止,我没有像这样使用一系列 map() 成功地做到这一点:
const result = appoint.map(elem => elem.b.)
const finalresult = result.map(
item =>item.filter(
(item, value)=> item[value] === false )
)
最佳答案
您可以先使用 map
获取一个仅包含 b
的新数组,然后使用 reduce
并在 reduce 回调中使用 for..in
迭代对象并获取为真的键
const appoint = [{
a: "asas",
b: {
au: false,
h: false,
l: true
}
},
{
a: "avd",
b: {
au: true,
h: false,
l: true
}
},
{
a: "as",
b: {
au: true,
h: false,
l: false
}
}
];
let filtered = appoint.map(function(item) {
return item.b;
}).reduce(function(acc, curr) {
for (let keys in curr) {
if (curr[keys]) {
acc.push(keys)
}
}
return acc;
}, []);
console.log(filtered)
关于javascript - 过滤对象数组中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56807607/