简体   繁体   English

使用下划线 js 或 lodash 从数组中删除对象

[英]Remove objects from an array using underscore js or lodash

I want to remove an object contains 'twitter' from the 'contents' array using underscore js.我想使用下划线 js 从 'contents' 数组中删除一个包含 'twitter' 的对象。

contents = 
[
  {
     "facebook": "test",
     "preview_image_url": "url",
     "preview_title": "title",
     "preview_description": "description"
  },
  {
     "twitter": "test",
     "preview_image_url": ""
  }
]

Expecting result array as follows预期结果数组如下

contents = 
[
  {
     "facebook": "test",
     "preview_image_url": "url",
     "preview_title": "title",
     "preview_description": "description"
  }
]

Your help is much appreciated.非常感谢您的帮助。

In case you ever want a vanilla JS version:如果你想要一个香草 JS 版本:

var filtered = contents.filter(function (obj) {
  return Object.keys(obj).indexOf('twitter') === -1;
});

Or, taking on TJ's comment:或者,接受 TJ 的评论:

var filtered = contents.filter(function (obj) {
  return !obj.hasOwnProperty('twitter');
});

使用remove()

_.remove(contents, _.ary(_.partialRight(_.has, 'twitter'), 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用lodash或underscore.js在javascript中处理对象数组 - Manipulation of array of objects in javascript using lodash or underscore.js 使用 Lodash 从嵌套数组中删除对象 - Remove objects from nested array using Lodash 使用下划线js或lodash将对象解析为数组 - Parsing object to array using underscore js or lodash Javascript / underscore / lodash:在对象数组中比较对象并删除匹配的对象 - Javascript/underscore/lodash : Comparing object in array of objects and remove the matched object 如何使用javascript或lodash从对象数组中删除不匹配的对象 - How to remove unmatched objects from array of objects using javascript or lodash 使用Underscore(或Lodash),如何从另一个数组中删除数组的每个成员? - Using Underscore (or Lodash), how do you remove every member of an array from another array? 独特的对象数组,并使用lodash或下划线合并重复项 - Unique array of objects and merge duplications using lodash or underscore 如何使用Underscore / LoDash将对象数组分成2个数组 - How to break an array of objects into 2 arrays, using Underscore/LoDash 使用lodash / underscore.js从数组获取对象属性和值 - Get object properties and values from array using lodash/underscore.js 使用lodash或下划线js查找数组中字符串元素的出现次数 - Find number of occurences of string elements in an Array using lodash or underscore js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM