remove objects from array by object property

前端 未结 13 2961
抹茶落季
抹茶落季 2020-11-22 17:40
var listToDelete = [\'abc\', \'efg\'];

var arrayOfObjects = [{id:\'abc\',name:\'oh\'}, // delete me
                      {id:\'efg\',name:\'em\'}, // delete me
            


        
13条回答
  •  被撕碎了的回忆
    2020-11-22 18:02

    You can use filter. This method always returns the element if the condition is true. So if you want to remove by id you must keep all the element that doesn't match with the given id. Here is an example:

    arrayOfObjects = arrayOfObjects.filter(obj => obj.id != idToRemove)

提交回复
热议问题