对象数组
{
first : { label :"a" , status : true , text: "dummy" },
second : { label :"b" , status : true , text: "dummy" },
third : { label :"c" , status : false , text: "dummy" }
}
我有对象和映射并推送到其他对象,检查对象键是否有标签是“c”,如果存在则不要推送
const newData = data && data.map(item => {
if(item.label !=='c') {
return {
...item,
};
}
});
预期结果
{
first : { label :"a" , status : true , text: "dummy" },
second : { label :"b" , status : true , text: "dummy" }
}
映射始终返回相同长度的新数组。您可以使用过滤器或减少来获得结果。
const newData = data && data.filter(item => item.label !== 'c');
或
const newData = data && data.reduce((acc, item) => {
if (item.label !== 'c') {
acc[item.label] = item;
}
return ACC;
}, {});