13

I have JSON array like this

var array= [{id:1,name:'foo'},{id:2,name:'bar'}]

I would like to add a new key (eg:isApproved) to each object in the existing array

expected output:

var array= [{id:1,name:'foo',isApproved:true},{id:2,name:'bar',isApproved:true}] 

I used the map function to achieve this

array.map(function(e,index){
     e.isApproved[index]= true
}

But this not worked for me

5
  • You have to return it from the callback. and map returns a new array. Commented Mar 14, 2018 at 9:20
  • just add return e after setting the value. Commented Mar 14, 2018 at 9:22
  • But getting Cannot set property '0' of undefined Commented Mar 14, 2018 at 9:22
  • try e.isApproved = true instead using e.isApproved[index] = true. Commented Mar 14, 2018 at 9:22
  • e.isApproved = true. delete [index] Commented Mar 14, 2018 at 9:22

6 Answers 6

21

You were really close. You do not need the index here. The map passes through every element of the array, so 'e' will be each object in your array.

var array= [{id:1,name:'foo'},{id:2,name:'bar'}];

array.map(function(e){
     e.isApproved = true;
});
          
console.log(array);

Sign up to request clarification or add additional context in comments.

1 Comment

Array.prototype.map() expects a return value from a function
10

With this code, you wont mutate objects inside your array

const arr = [{id:1,name:'foo'},{id:2,name:'bar'}];
const mapped = arr.map(element => Object.assign(element, {isApproved: true})

More new approach would be using spread operator:

const arr = [{id:1,name:'foo'},{id:2,name:'bar'}];
const mapped = arr.map(element => ({isApproved: true ,...element}))

Snippet

const arr = [{id:1,name:'foo'},{id:2,name:'bar'}];
    const mapped = arr.map(element => ({isApproved: true ,...element}))


console.log(mapped)

1 Comment

Thanks about the spread operator, fulfilled my leant quota for the day.
2

Try this, you don't need the index:

var array= [{id:1,name:'foo'},{id:2,name:'bar'}];

array.map(value => value.isApproved = true);
console.log(array)

Comments

2

Use the actual item 'e' in the map

Map also gives you the facility to alter each element and return them in a new array. This can be useful if you do not want your current array to alter its state rather you need a modified form of the same array.

check this code:

var array= [{id:1,name:'foo'},{id:2,name:'bar'}];

var modifiedArray = array.map(function(e,index){
    return Object.assign({isApproved:true},e);
});

console.log(array);
console.log(modifiedArray);

Output:

//array
[{id: 1, name: "foo"},
{id: 2, name: "bar"}]

//modifiedArray
[{isApproved: true, id: 1, name: "foo"},
{isApproved: true, id: 2, name: "bar"}]

Comments

0

e[index] doesn't make sense since this index is meant for the array you are iterating.

Set the property directly to e

array.map(function(e,index){
     e.isApproved = true; 
}

Comments

0
<script>

var array= [{id:1,name:'foo'},{id:2,name:'bar'}]
array.forEach(function(e,index){
     e.isApproved= true;
})
console.log(array);

</script>

You can use forEach instead of map.

1 Comment

actually, its always better to use map, if you want to modify every element of given array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.