Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two object data become one

I two data need to merge become one. My example data like this..

First data

{Address: "Test", City: "test", State: "Test"}

Second Data

{UserId:"John", Name: "John"}

When I use this method const a = [].concat(this.firstData, this.secondData); it become in array not combine become one, example like bellow:

(2)[{…}, {…}]

[0]{Address: "Test", City: "test", State: "Test", …}
[1]{UserId:"John", Name: "John", …}

What method should I use to merge become one

like image 647
Shah Avatar asked Feb 02 '26 21:02

Shah


1 Answers

You can use the spread operator on both:

const firstData = {Address: "Test", City: "test", State: "Test"};
const secondData = {UserId:"John", Name: "John"};

const merged = { ...firstData, ...secondData };

console.log(merged)

Useful references:

  • Docs for Variable Declarations/Spread in TypeScript Handbook.
  • Docs for JavaScript's Spread Operator (MDN).
like image 186
acdcjunior Avatar answered Feb 05 '26 12:02

acdcjunior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!