Javascript 删除嵌套数组中的空对象
我有一个数组Javascript 删除嵌套数组中的空对象,javascript,arrays,object,Javascript,Arrays,Object,我有一个数组 const exampleArray = [ { id: 1, name: test }, { id: 1, name: test }, { }, ] 现在数组exampleArray位于另一个对象数组中,因此 const exampleNest = [{ property1: {}, property2: [], exampleArray: [{...}], }]
const exampleArray = [
{
id: 1,
name: test
},
{
id: 1,
name: test
},
{
},
]
现在数组exampleArray
位于另一个对象数组中,因此
const exampleNest = [{
property1: {},
property2: [],
exampleArray: [{...}],
}]
我需要删除exampleArray
中的空对象,然后返回数组的其余部分
我已经试过使用过滤器了
const noEmptyObjects = exampleNest.filter(({ exampleArray }) =>
exampleArray.filter(attachment => attachment !== {}),
);
我也尝试了
Object.keys(附件).length!==0
而不是附件!=={}
但是我继续接收带有空项的数组 您可以通过检查对象是否具有id属性来筛选数组
exampleArray.filter(函数(obj){return obj.hasOwnProperty(“id”);})
,因为{}是对象。所以,{}=={}总是错误的
exampleArray.filter(attachment => JSON.stringify(attachment) !== "{}")
您可以使用simple来检查空对象,并使用该对象查找数组
exampleArray.filter(attachment => (condition from above link));
这不是一个有效的JS:`我有一个数组
const exampleNest=[property1:{},property2:[],exampleArray:[{…}],]
你是指数组还是对象?附件!=={}
将永远不会返回false
感谢您的更正,这是一个对象数组:)