[英]Manipulation of array of objects in javascript using lodash or underscore.js
[英]Remove objects from an array using underscore js or lodash
我想使用下划線 js 從 'contents' 數組中刪除一個包含 'twitter' 的對象。
contents =
[
{
"facebook": "test",
"preview_image_url": "url",
"preview_title": "title",
"preview_description": "description"
},
{
"twitter": "test",
"preview_image_url": ""
}
]
預期結果數組如下
contents =
[
{
"facebook": "test",
"preview_image_url": "url",
"preview_title": "title",
"preview_description": "description"
}
]
非常感謝您的幫助。
如果你想要一個香草 JS 版本:
var filtered = contents.filter(function (obj) {
return Object.keys(obj).indexOf('twitter') === -1;
});
或者,接受 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.