Combine two arrays into a combined key-value pairs object
Thu Apr 28 2022 08:44:56 GMT+0000 (Coordinated Universal Time)
Saved by
@jen_magpantay
#javascript
// if using a forEach() to associate each key-value pairs into an array of objects
function combiningArraysIntoKeyPairValues() {
var arr1 = 'declare your first array here';
var arr2 = 'declare your second array here';
// ps: you must consider that both array has same length?
var combinedObj = {};
let arr = [];
arr1.forEach((item, index) => {
combinedObj = { name: item, amount: arr2[index] };
arr.push(combinedObj);
});
return arr;
}
console.log(combiningArraysIntoKeyPairValues());
content_copyCOPY
Comments