Mathew Wade
Mathew Wade

Reputation: 1

Merging two array of objects in node js

Input:

const name = [{id:1, first_name: "Sade", age:"30"}, {id:2, first_name: "Jon", age:"40"}];
const ln = [{last_name: "Smith"}, {last_name: "Doe"}];

Output:

const name = [
  {id:1, first_name: "Sade", last_name: "Smith", age:"30"},
  {id:2, first_name: "Jon", last_name: "Doe", age"40"}
];

Please help me to get this output in nodejs

Upvotes: 0

Views: 51

Answers (1)

Mohammad Usman
Mohammad Usman

Reputation: 39322

You can use .map() and Spread Syntax to get the desired output:

const name = [{id:1, first_name: "Sade"}, {id:2, first_name: "Jon"}];
const lname = [{last_name: "Smith"}, {last_name: "Doe"}];

const output = name.map((o, i) => ({...o, ...lname[i]}));

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions