Remove Object from Array using JavaScript


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

someArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"John", lines:"1,19,26,96"}];

I want to achieve:

someArray = [{name:"John", lines:"1,19,26,96"}];

This question is tagged with javascript arrays

~ Asked on 2012-04-05 08:07:51

The Best Answer is


You can use several methods to remove item(s) from an Array:

someArray.shift(); // first element removed
someArray = someArray.slice(1); // first element removed
someArray.splice(0, 1); // first element removed
someArray.pop(); // last element removed
someArray = someArray.slice(0, a.length - 1); // last element removed
someArray.length = someArray.length - 1; // last element removed

If you want to remove element at position x, use:

someArray.splice(x, 1);


someArray = someArray.slice(0, x).concat(someArray.slice(-x));

Reply to the comment of @chill182: you can remove one or more elements from an array using Array.filter, or Array.splice combined with Array.findIndex (see MDN), e.g.

// non destructive filter > noJohn = John removed, but someArray will not change_x000D_
let someArray = getArray();_x000D_
let noJohn = someArray.filter( el => !== "John" ); _x000D_
log("non destructive filter > noJohn = ", format(noJohn));_x000D_
log(`**someArray.length ${someArray.length}`);_x000D_
// destructive filter/reassign John removed > someArray2 =_x000D_
let someArray2 = getArray();_x000D_
someArray2 = someArray2.filter( el => !== "John" );_x000D_
log("", "destructive filter/reassign John removed > someArray2 =", _x000D_
log(`**someArray2.length ${someArray2.length}`);_x000D_
// destructive splice /w findIndex Brian remains > someArray3 =_x000D_
let someArray3 = getArray();_x000D_
someArray3.splice(someArray3.findIndex(v => === "Kristian"), 1);_x000D_
someArray3.splice(someArray3.findIndex(v => === "John"), 1);_x000D_
log("", "destructive splice /w findIndex Brian remains > someArray3 =", _x000D_
log(`**someArray3.length ${someArray3.length}`);_x000D_
// Note: if you're not sure about the contents of your array, _x000D_
// you should check the results of findIndex first_x000D_
let someArray4 = getArray();_x000D_
const indx = someArray4.findIndex(v => === "Michael");_x000D_
someArray4.splice(indx, indx >= 0 ? 1 : 0);_x000D_
log("", "check findIndex result first > someArray4 (nothing is removed) > ",_x000D_
log(`**someArray4.length (should still be 3) ${someArray4.length}`);_x000D_
function format(obj) {_x000D_
  return JSON.stringify(obj, null, " ");_x000D_
function log(...txt) {_x000D_
  document.querySelector("pre").textContent += `${txt.join("\n")}\n`_x000D_
function getArray() {_x000D_
  return [ {name: "Kristian", lines: "2,5,10"},_x000D_
           {name: "John", lines: "1,19,26,96"},_x000D_
           {name: "Brian", lines: "3,9,62,36"} ];_x000D_

~ Answered on 2012-04-05 08:11:37


The clean solution would be to use Array.filter:

var filtered = someArray.filter(function(el) { return el.Name != "Kristian"; }); 

The problem with this is that it does not work on IE < 9. However, you can include code from a Javascript library (e.g. underscore.js) that implements this for any browser.

~ Answered on 2012-04-05 08:11:44

Most Viewed Questions: