Remove Object from Array using JavaScript

前端 未结 29 2090
南笙
南笙 2020-11-22 10:24

How can I remove an object from an array? I wish to remove the object that includes name Kristian from someArray. For example:

som         


        
29条回答
  •  北海茫月
    2020-11-22 10:54

    You could use array.filter().

    e.g.

            someArray = [{name:"Kristian", lines:"2,5,10"},
                         {name:"John", lines:"1,19,26,96"}];
    
            someArray = someArray.filter(function(returnableObjects){
                   return returnableObjects.name !== 'Kristian';
            });
    
            //someArray will now be = [{name:"John", lines:"1,19,26,96"}];
    

    Arrow functions:

    someArray = someArray.filter(x => x.name !== 'Kristian')
    

提交回复
热议问题