add a key value pair to an array of objects javascript code example

Example 1: 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 2: add new key value pair to object javascript

const person = {
	name: "Elon Musk"
}

person.age = 49;

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