I have an array of objects
const arr1 = [{depCode: "Dep code", typeCode: "Code", number: "Number"},
{depCode: "123", typeCode: "12", number: "35"}];
How can I map them to an array of objects like below
expected output = [{depCode:{key: "Dep code", value: "123"}},
typeCode:{key: "Code", value: "12"},
number:{key: "Number", value: "35"}]
const obj1 = {depCode: "Dep code", typeCode: "Code", number: "Number"};
const obj2= {depCode: "123", typeCode: "12", number: "35"};
const arr = [];
Object.keys(obj1).forEach(a=> arr.push({[a]: obj2[a]}));
console.log(arr);
Since both obj1 and obj2 have similar schema,
let obj1 = {depCode: "Dep code", typeCode: "Code", number: "Number"};
let obj2 = {depCode: "123", typeCode: "12", number: "35"};
let objectKeys = Object.keys(obj1);
let resArray = objectKeys.reduce((prevValue, currValue)=>{
prevValue.push({[currValue]: {key:obj1[currValue],value:obj2[currValue]}});
return prevValue;
}, [])
console.log(resArray);
Here is the jsfiddle for the same
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.