0

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

Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
  • Hello, welcome to StackOverflow. In order to increase your chances of receiving a positive response to your query please read and consider the points raised in the [how to ask](https://stackoverflow.com/help/how-to-ask) section of this site. – Andrew Hardiman Jan 14 '21 at 08:57

1 Answers1

1

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; }
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
  • @MathewWade It will work with your new method as well. Try it. – Mohammad Usman Jan 14 '21 at 08:52
  • but then the ln[] is adding at the end, i want before the age attribute – Mathew Wade Jan 14 '21 at 08:56
  • @MathewWade Order of properties in an object doesn't matter. If you need properties in a specific order, use array then. See this post for more information https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order#:~:text=Most%20Browsers%20iterate%20object%20properties,this%20and%20all%20browsers%20comply) – Mohammad Usman Jan 14 '21 at 08:57
  • Is it possible to get output within same array name?? – Mathew Wade Jan 14 '21 at 09:32