-10

enter image description here

I have an array with multiple objects.How can i delete an element in a object.

For example i have attached a image in that i need to delete organization element.

Please help me out to fix.

Rajesh Bunny
  • 329
  • 1
  • 2
  • 7

3 Answers3

1

var a = {name: 'pardeep', 'sirname': 'jain'}
delete a['name']
console.log(a);

Just use delete keyword for the key like this -

delete objectName['keyName']
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
0

Use the delete operator

const deleteAttribute = (array, attribute) => array.map(x => {
  // Delete the attribute here
  delete x[attribute];
  return x;
});

const arr = [{
  attr: 'hello',
  color: 'red',
  organization: 'Good'
}, {
  attr: 'world',
  color: 'blue',
  organization: 'bad'
}];

console.log(deleteAttribute(arr, 'organization'));
Community
  • 1
  • 1
molamk
  • 4,076
  • 1
  • 13
  • 22
0

You can use map and delete like below

var arr = [{
  name: 'hi',
  test: 'x',
  organization: 'X'
}, {
  name: 'guys',
  test: 'y',
  organization: 'Y'
}];

  console.log(arr.map(x => { delete x.organization; return x}));
dileepkumar jami
  • 2,185
  • 12
  • 15