Home > Blockchain >  javascript : Remove item from list of dictionary with name
javascript : Remove item from list of dictionary with name

Time:08-25

How to Remove item from list of dict based on list of names

arr1 = [{name:'item0',value:0},{name:'item1',value:1},{name:'item2',value:2},{name:'item3',value:3}

itemsToRemove = ['item1','item2']

expected Output

arr1 = [{name:'item0',value:0},{name:'item3',value:3}]

tried to splice but doesn't work

arr1.splice(columnsToRemove[1],1)

CodePudding user response:

You can use Array.prototype.filter() to clean it.

let arr1 = [{name:'item0',value:0},{name:'item1',value:1},{name:'item2',value:2},{name:'item3',value:3}];

const itemsToRemove = ['item1','item2'];

arr1 = arr1.filter(obj => !itemsToRemove.includes(obj.name));

console.log(arr1);

  • Related