Add Key and Value
Write a function called addKeyAndValue which accepts an array of objects and returns the array of objects passed to it with each object now including the key and value passed to the function.
Example:
var arr = [{name: ‘Elie’}, {name: ‘Tim’}, {name: ‘Matt’}, {name: ‘Colt’}];
console.log(JSON.stringify(addKeyAndValue(arr, ‘title’, ‘Instructor’),null,2))
“[
{
"name": "Elie",
"title": "Instructor"
},
{
"name": "Tim",
"title": "Instructor"
},
{
"name": "Matt",
"title": "Instructor"
},
{
"name": "Colt",
"title": "Instructor"
}
]”
function addKeyAndValue(arr, key, value){
return arr.reduce(function(acc, next, idx){
acc[idx][key] = value;
return acc;
}, arr);
}
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}];
console.log(JSON.stringify(addKeyAndValue(arr, 'title', 'Instructor'),null,2))
We create a function called addKeyAndValue and we create parameter arr and key and value
We return arr.reduce(function(acc,next,idx) {}) arr is an array that hold key value pairs inside and with reduce we chain .reduce and have callback function that accepts mostly 2 cases buy in our case we need the index case. those are acc,next,idx
acc[idx][key] = value; We take the key value pair of some index acc holds all the data , [idx] gives specivic index we need and from that data and that index we have that [key] and it will equal to value;
return acc; We return corrected key value paird new acc or accumulator