'Add key value pair to all objects in array
I wanted to add a key:value parameter to all the objects in an array.
eg:
var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
Now I wanted to add a new parameter, isActive to all the objects so the resulting array will look like.
eg:
[{
name: 'eve',
isActive: true
}, {
name: 'john',
isActive: true
}, {
name: 'jane',
isActive: true
}]
I can always loop through the array and insert a key,value pair. But was wondering if there was a better way to do so
Solution 1:[1]
You can do this with map()
var arrOfObj = [{
name: 'eve'
}, {
name: 'john'
}, {
name: 'jane'
}];
var result = arrOfObj.map(function(o) {
o.isActive = true;
return o;
})
console.log(result)
If you want to keep original array you can clone objects with Object.assign()
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);
Solution 2:[2]
I would be a little cautious with some of the answers presented in here, the following examples outputs differently according with the approach:
const list = [ { a : 'a', b : 'b' } , { a : 'a2' , b : 'b2' }]
console.log(list.map(item => item.c = 'c'))
// [ 'c', 'c' ]
console.log(list.map(item => {item.c = 'c'; return item;}))
// [ { a: 'a', b: 'b', c: 'c' }, { a: 'a2', b: 'b2', c: 'c' } ]
console.log(list.map(item => Object.assign({}, item, { c : 'c'})))
// [ { a: 'a', b: 'b', c: 'c' }, { a: 'a2', b: 'b2', c: 'c' } ]
I have used node v8.10.0 to test the above pieces of code.
Solution 3:[3]
You may also try this:
arrOfObj.forEach(function(item){item.isActive = true;});
console.log(arrOfObj);
Solution 4:[4]
@Redu's solution is a good solution
arrOfObj.map(o => o.isActive = true;) but Array.map still counts as looping through all items.
if you absolutely don't want to have any looping here's a dirty hack :
Object.defineProperty(Object.prototype, "isActive",{
value: true,
writable: true,
configurable: true,
enumerable: true
});
my advice is not to use it carefully though, it will patch absolutely all javascript Objects (Date, Array, Number, String or any other that inherit Object ) which is really bad practice...
Solution 5:[5]
arrOfObj.map(o => {
o.isActive = true;
return o;
});
Solution 6:[6]
_.forEach(arrOfObj,(arrVal,arrIn) => {
arrVal.isAcitve = true;
}
Solution 7:[7]
var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
const result = arrOfObj.map((name) => {
name["isActive"] = true;
return name;
});
console.log(result);
Solution 8:[8]
Looping through the array and inserting a key, value pair is about your best solution. You could use the 'map' function but it is just a matter of preference.
var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
arrOfObj.map(function (obj) {
obj.isActive = true;
});
Solution 9:[9]
Simply use map function:
var arrOfObj = arrOfObj.map(function(element){
element.active = true;
return element;
}
Map is pretty decent on compatibility: you can be reasonably safe from IE <= 9.
However, if you are 100% sure your users will use ES6 Compatible browser, you can shorten that function with arrow functions, as @Sergey Panfilov has suggested.
Solution 10:[10]
var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
var injectObj = {isActive:true, timestamp:new Date()};
// function to inject key values in all object of json array
function injectKeyValueInArray (array, keyValues){
return new Promise((resolve, reject) => {
if (!array.length)
return resolve(array);
array.forEach((object) => {
for (let key in keyValues) {
object[key] = keyValues[key]
}
});
resolve(array);
})
};
//call function to inject json key value in all array object
injectKeyValueInArray(arrOfObj,injectObj).then((newArrOfObj)=>{
console.log(newArrOfObj);
});
Output like this:-
[ { name: 'eve',
isActive: true,
timestamp: 2017-12-16T16:03:53.083Z },
{ name: 'john',
isActive: true,
timestamp: 2017-12-16T16:03:53.083Z },
{ name: 'jane',
isActive: true,
timestamp: 2017-12-16T16:03:53.083Z } ]
Solution 11:[11]
let arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
let newArr = arrOfObj.map(i => ({...i,isActive: true}));
Here what i did was i spread the current object and insert a new key with a value to the object and return it as a new object.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
