js如何筛选出数组中的对象中的某个值等于true的数组
可以使用数组的filter方法来筛选出符合条件的对象。可以使用对象的属性来判断某个值是否为true,然后返回过滤后的数组。
以下是一个示例代码:
const arr = [
{ name: 'Alice', active: true },
{ name: 'Bob', active: false },
{ name: 'Charlie', active: true },
{ name: 'David', active: false }
];
const filteredArr = arr.filter(obj => obj.active === true);
console.log(filteredArr);
// 输出结果: [{ name: 'Alice', active: true }, { name: 'Charlie', active: true }]
在上面的示例中,filter方法会迭代数组arr的每个元素,并将符合条件的元素添加到filteredArr数组中。在这个例子中,我们通过判断active属性是否为true来筛选出满足条件的对象。最后,filteredArr数组只包含了active属性值为true的对象
原文地址: https://www.cveoy.top/t/topic/hH1Q 著作权归作者所有。请勿转载和采集!