add key value pair to array of object javascript code example

Example 1: map add key to object in array javascript

const newArr = [
  {name: 'eve'},
  {name: 'john'},
  {name: 'jane'}
].map(v => ({...v, isActive: true}))

Example 2: add key value pair to all objects in array

var arrOfObj = [{
  name: 'eve'
}, {
  name: 'john'
}, {
  name: 'jane'
}];

var result = arrOfObj.map(function(el) {
  var o = Object.assign({}, el);
  o.isActive = true;
  return o;
})

console.log(arrOfObj);
console.log(result);

Example 3: add new key value pair to object javascript

const person = {
	name: "Elon Musk"
}

person.age = 49;

console.log(person);
// {name: "Elon Musk", age: 49}