2

I'm learning ES6 and objects and want to know how to get rid of one key-value pair in this array of objects:

[
    { "name" : "mark", "height" : "tall", "theId" : "1", "nat" : "uk"},
    { "name" : "ben", "height" : "medium", "theId" : "2", "nat" : "uk"},
    { "name" : "neil", "height" : "small", "theId" : "3", "nat" : "uk" }
]

The result should be:

[
    { "name" : "mark", "height" : "tall", "nat" : "uk"},
    { "name" : "ben", "height" : "medium", "nat" : "uk"},
    { "name" : "neil", "height" : "small", "nat" : "uk" }
]

I created a forEach function and tried to push each result into a new array, but now there are no objects.

How can this be fixed? Or is there a better way of doing this with the ES6/ES7 syntax? Thanks for any help. The code and codePen URL are below:

Codepen: https://codepen.io/anon/pen/bPYQyb

let objArr = [
 { "name" : "mark", "height" : "tall", "theId" : "1", "nat" : "uk"},
 { "name" : "ben", "height" : "medium", "theId" : "2", "nat" : "uk"},
 { "name" : "neil", "height" : "small", "theId" : "3", "nat" : "uk" }
],
    arr = [];

objArr.forEach(function(obj) {
 for (let column in obj) {
      let currArr = [];
    if (isNaN(obj[column])) {
      console.log('true');
      currArr.push(obj[column]);
    }
    arr.push(currArr);
  }
});

console.log(arr);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
user8758206
  • 2,106
  • 4
  • 22
  • 45

6 Answers6

6
objArr.forEach(a => delete a.theId);

Docs for delete operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

Siri
  • 1,117
  • 6
  • 13
4

Just use destructuring and spreading with map and the implicit return of an arrow function:

let objArr = [{"name":"mark","height":"tall","theId":"1","nat":"uk"},{"name":"ben","height":"medium","theId":"2","nat":"uk"},{"name":"neil","height":"small","theId":"3","nat":"uk"}];

const res = objArr.map(({ theId, ...r }) => r);

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
3

Just delete the key:

let objArr = [
 { "name" : "mark", "height" : "tall", "theId" : "1", "nat" : "uk"},
 { "name" : "ben", "height" : "medium", "theId" : "2", "nat" : "uk"},
 { "name" : "neil", "height" : "small", "theId" : "3", "nat" : "uk" }
];


objArr.forEach(function(obj) {
 delete obj.theId;
});

console.log(objArr);
3

If you want to modify the current objects in-place, you can loop through the objects in the array and use the delete <name_of_key>; statement (doc here). It is safe to use even if the key is not present in the object.

So you could do

for (let obj of objArr) {
  delete obj.theId;
}

or the functional way

objArr.forEach(obj => delete obj.theId);
jonathanGB
  • 1,500
  • 2
  • 16
  • 28
1

If immutability is important, then one approach would be to .map() each item from objArray to a new arr array where, during the map callback, you would filter out the entry with key: "theId".

To filter out the "theId" key, iterate the keys of the current item being mapped via a for(.. in ..) loop, check that the key is not "thisId", and if that be the case, add it and it's value (ie item[key]) to a new object:

const objArr = [{
    "name": "mark",
    "height": "tall",
    "theId": "1",
    "nat": "uk"
  },
  {
    "name": "ben",
    "height": "medium",
    "theId": "2",
    "nat": "uk"
  },
  {
    "name": "neil",
    "height": "small",
    "theId": "3",
    "nat": "uk"
  }
]

const arr = objArr.map(item => {

  /* arrItem is the new object that 
  item will be mapped to. This object
  will exclude the key "theId" */
  const arrItem = {};
  
  /* Iterate keys of item */
  for (const key in item) {

    if (key !== 'theId') {
    
      /* If key not "theId" then add
      it and the entry value to 
      arrItem */      
      arrItem[key] = item[key];
    }
  }

  /* Return arrItem */
  return arrItem;
});


console.log(arr);
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
0

Try this:

const data  = [
    { "name" : "mark", "height" : "tall", "theId" : "1", "nat" : "uk"},
    { "name" : "ben", "height" : "medium", "theId" : "2", "nat" : "uk"},
    { "name" : "neil", "height" : "small", "theId" : "3", "nat" : "uk" }
]

  const result = data.map(({theId, ...rest}) => rest);
console.log(result);
Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23