[英]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.