如何删除具有属性的数组对象 [英] How to remove array objects having property

查看:23
本文介绍了如何删除具有属性的数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从具有特定属性 = x 的数组中删除所有对象的最简单方法是什么?

What is the easiest way to remove all objects from an array with a particular property = x?

推荐答案

使用 _.filter

var result = _.filter(arr, function(item) {
  return !("prop" in item);
});

如果要将其限制为每个项目的直接属性,请使用

If you want to limit it to the immediate properties of each item, use

var result = _.filter(arr, function(item) {
  return !item.hasOwnProperty("prop");
});

这篇关于如何删除具有属性的数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆