Remove Object from Array using JavaScript

前端 未结 29 2829
南笙
南笙 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 10:44

    This answer

    for (var i =0; i < someArray.length; i++)
       if (someArray[i].name === "Kristian") {
          someArray.splice(i,1);
       }
    

    is not working for multiple records fulfilling the condition. If you have two such consecutive records, only the first one is removed, and the other one skipped. You have to use:

    for (var i = someArray.length - 1; i>= 0; i--)
       ...
    

    instead .

提交回复
热议问题