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

1
  • 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 section of this site. Commented Jan 14, 2021 at 8:57

1 Answer 1

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

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

4 Comments

@MathewWade It will work with your new method as well. Try it.
but then the ln[] is adding at the end, i want before the age attribute
@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 stackoverflow.com/questions/5525795/…)
Is it possible to get output within same array name??

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.