Skip to content

Instantly share code, notes, and snippets.

@surajitbasak109
Last active October 14, 2019 08:24
Show Gist options
  • Save surajitbasak109/96cd7e90316116f8140fd9585506238a to your computer and use it in GitHub Desktop.
Save surajitbasak109/96cd7e90316116f8140fd9585506238a to your computer and use it in GitHub Desktop.

Remove Object from Array using JavaScript

How can I remove an object from an array? I wish to remove the object that includes name Ketty 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"}];

A simple Solution

var someArray = [
  {name:"Kristian", lines:"2,5,10"},
  {name:"John", lines:"1,19,26,96"},
  {name:"Ketty", lines:"20,25,26,96"},
  {name:"Antony", lines:"1,19,26,96"},
];
var index = someArray.findIndex(x => x.name == "Ketty");

console.log(index);
someArray.splice(index, 1);
console.log(someArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment