0

my first object like this :

{id: 1, quantity: 10, address: {id: 2, name: "stamford bridge", city: "london"}}

my second object like this :

{data: {id: 2, quantity: 20, address: {id: 4, name: "old traford", city: "manchester"}}, expired: "2017-08-16T06:46:02.566Z"}

I want to combine the object

How can I do it?

2
  • 3
    What do you mean by "group the object"? What is expected result? See stackoverflow.com/help/mcve Commented Aug 16, 2017 at 6:21
  • Can you give the expected output? Commented Aug 16, 2017 at 6:41

2 Answers 2

2

How can I merge properties of two JavaScript objects dynamically?

This method can combine the properties of two objects together.

Object.assign(obj1, obj2);

Or you have to change your data structure, if you're thinking about arrays.

var x = {id: 1, quantity: 10, address: {id: 2, name: "stamford bridge", city: "london"}}

and

var y = {data: [{id: 2, quantity: 20, address: {id: 4, name: "old traford", city: "manchester"}}], expired: "2017-08-16T06:46:02.566Z"}

Then you

y.data.push(x)
Sign up to request clarification or add additional context in comments.

Comments

0

we can use the Object.assign(obj1, obj2); function to merge or else please add the sample output format you are expecting

var x={id: 1, quantity: 10, address: {id: 2, name: "stamford bridge", city: "london"}}

var y={data: {id: 2, quantity: 20, address: {id: 4, name: "old traford", city: "manchester"}}, expired: "2017-08-16T06:46:02.566Z"}

var z = Object.assign(x, y);

console.log(z)
console.log(z.id)
console.log(z.data)
console.log(z.data.id)
 

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.