我有以下代码段.我想使用具有键的数组删除键值对.
var fruits = [{ fruitId: B1, fruitName:"Banana", size : Medium}, { fruitId: B2, fruitName: "Orange", size : Big}, { fruitId: B3, fruitName: "Lemon", size: small}]; var fruitIds = [B1, B2];
现在我想在使用fruitIds数组切片后切割fruits数组.切片后我想要结果
var fruits = [{ fruitName: "Lemon", size: small}];
我需要它在纯Javascript中.
你可以利用Array.prototype.filter
:
var fruits = [
{
fruitName : "Banana",
size : 'Medium'
},
{
fruitName : "Orange",
size : 'Big'
},
{
fruitName : "Lemon",
size : 'small'
}
];
var fruitNames = ['Banana', 'Orange'];
var result = fruits.filter(function (x) {
return fruitNames.indexOf(x.fruitName) === -1;
});
document.body.innerHTML = "" + JSON.stringify(result, null, 4) + "
";