使用javascript根据对象的属性从数组中删除对象

给定对象数组:

var items = [
{"rank":"3","color":"red"},
{"rank":"4","color":"blue"},
{"rank":"0","color":"green"},
{"rank":"6","color":"blue"},
{"rank":"0","color":"yellow"}
];

我想删除所有排名为0的条目,因此结果将是:

项目:

[{"rank":"3","color":"red"},
{"rank":"4","color":"blue"},
{"rank":"6","color":"blue"}];
评论
Buck
Buck

Try to filter by trusy value and apply + sign to convert from string to number:

var items = [
   {"rank":"3","color":"red"},
   {"rank":"4","color":"blue"},
   {"rank":"0","color":"green"},
   {"rank":"6","color":"blue"},
   {"rank":"0","color":"yellow"}
];

result = items.filter(({rank})=> +rank);
console.log(result);
点赞
评论
纯情小火鸡
纯情小火鸡

应用过滤器:

var items = [{"rank":"3","color":"red"},{"rank":"4","color":"blue"},{"rank":"0","color":"green"},{"rank":"6","color":"blue"},{"rank":"0","color":"yellow"}];

result = items.filter(({rank})=>rank>0);
console.log(result);
点赞
评论
静静的思念
静静的思念
var items = [
{"rank":"3","color":"red"},
{"rank":"4","color":"blue"},
{"rank":"0","color":"green"},
{"rank":"6","color":"blue"},
{"rank":"0","color":"yellow"}
];

let filteredArray = items.filter(el => el.rank > 0);

console.log(filteredArray);
点赞
评论
non_et
non_et

您可以使用Array.filter方法执行此操作。

items = items.filter(i=> {
   return i.rank !== '0';
});
点赞
评论
布水风
布水风

您可以像这样应用过滤器:

items = items.filter(el => el.rank != "0")
点赞
评论
俄还好
俄还好

您可以将过滤器添加到Items数组:

items.filter(item => item.rank > 0)
点赞
评论