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!

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);

Sign up to request clarification or add additional context in comments.

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);

Comments

Your Answer

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.