Home > Software design >  how to make two object value into key and value
how to make two object value into key and value

Time:04-14

My Result

let a = [ { _id: '2022-04-08' } ] let b = [ { totalCount: 3 } ]

The result I want to get instead:

NDE { _id: '2022-04-08', count: 3 }

CodePudding user response:

Try this,

let a = [ { _id: '2022-04-08' } ] ;
let b = [ { totalCount: 3 } ];

obj = {};
obj[a[0]._id] = b[0].totalCount;
console.log(obj);

CodePudding user response:

You just use their key and value to define a new object. To merge 2 objects use ... operator

let a = [ { _id: '2022-04-08' } ];
let b = [ { totalCount: 3 } ];

const result = {[a[0]._id] : b[0].totalCount}
console.log(result)

//merging 2 objects
let obj = {...a[0], ...b[0]}
console.log(obj)

//merging 2 objects with new property name
let obj2 = {...a[0], count: b[0].totalCount}
console.log(obj2)

  • Related