Home > Mobile >  Remove object from array of objects based on value
Remove object from array of objects based on value

Time:02-10

I have this array of objects:

var data = [{
    "code": "8252",
    "name": "Věšák Sentini, antik mosaz",
  },
  {
    "code": "8253",
    "name": "Věšák Sentini, matný chrom",
  },
  {
    "code": "8254",
    "name": "Věšák Sentini, antik měď",
  },
  {
    "code": "8261",
    "name": "Věšák Kasper I, matný nikl",
  }
]

How can I remove the object from array that has code == 8254 please?

CodePudding user response:

You can use array.filter to get a new array without specified element:

var data = [{
    "code": "8252",
    "name": "Věšák Sentini, antik mosaz",
  },
  {
    "code": "8253",
    "name": "Věšák Sentini, matný chrom",
  },
  {
    "code": "8254",
    "name": "Věšák Sentini, antik měď",
  },
  {
    "code": "8261",
    "name": "Věšák Kasper I, matný nikl",
  }
]

let output = data.filter(x => x.code !== "8254");

console.log(output);

  • Related