我有一个数组 现在数组 我需要删除 我已经尝试使用过滤器 我也尝试了 答案 0 :(得分:1) 您可以使用简单的https://stackoverflow.com/a/32108184/3932166来检查空对象并对此数组进行检查。 答案 1 :(得分:0) 因为{}是对象。因此,{} == {}始终为假。 答案 2 :(得分:0) 您可以通过检查对象是否具有id属性来过滤数组 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(attachment).length !== 0
而不是attachment !== {}
,但是我继续收到带有空项目的数组。 3 个答案:
exampleArray.filter(attachment => (condition from above link));
exampleArray.filter(attachment => JSON.stringify(attachment) !== "{}")
exampleArray.filter(function(obj) { return obj.hasOwnProperty("id"); })