0

I need help with solution on this. I have two objects: Input:

obj1 = [{name:A}, {name:B}, {name:C}...,]
obj2 = [{value:1}, {value:2}, {value:3}...,]

And i need to make from these 2 objects one. Output:

obj3 = [{A:1}, {B:2}, {C:3}...,]

Anybody know how to merge obj and obj2 to one? Thanks!

pilchard
13.1k5 gold badges14 silver badges27 bronze badges
asked Apr 23, 2022 at 8:41
0

2 Answers 2

2

If you have consitent nameing, you could map with index.

const
 names = [{ name: 'A' }, { name: 'B' }, { name: 'C' }],
 values = [{ value: 1 }, { value: 2 }, { value: 3 }],
 result = names.map(({ name }, i) => ({ [name]: values[i].value }));
console.log(result);

answered Apr 23, 2022 at 8:51

Comments

1

You could do this

const obj1 = [{name:'A'}, {name:'B'}, {name:'C'}];
const obj2 = [{value:1}, {value:2}, {value:3}];
const merged = Array.from({length:Math.min(obj1.length, obj2.length)}, (_,i) => {
 return { [obj1[i].name]: obj2[i].value };
});
console.log(merged);

answered Apr 23, 2022 at 8:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.