Push object inside object javascript

My question is simple, I have 2 objects like this:

object1 = {
    content1: {}
}

object2 = {
    stuff: {},
    moreStuff: {}
}

And I want to add the content of object2 to content1 [which is inside of object1].

Like this:

object1 = {
    content1: {
        stuff: {},
        moreStuff: {}
    }
}

asked Feb 17, 2017 at 4:30

Diogo CapelaDiogo Capela

4,7044 gold badges23 silver badges33 bronze badges

This is very simple;

object1.content1 = object2

answered Feb 17, 2017 at 4:34

buræqueteburæquete

13.7k4 gold badges40 silver badges84 bronze badges

This will allow you to add an object inside another object.
With other examples you will get a substitution instead of adding. e.g.

const obj1 = {
	innerObj:{
  	name:'Bob'
  },
  innerOBj2:{
  	color:'blue'
  }
}

const obj2 = {
	lastName:'Some',
  age:45
}

obj1.innerObj = Object.assign[obj1.innerObj,obj2];
console.log[obj1];

Now if you need something more advance, you should take a look to some functional programming framework like ramda, which will allow you to merge object. R.merge.

answered Feb 17, 2017 at 4:48

HosarHosar

4,9133 gold badges24 silver badges34 bronze badges

Something keeping you from doing: object1.content1 = object2 ?

answered Feb 17, 2017 at 4:34

Kelvin De MoyaKelvin De Moya

4,5421 gold badge16 silver badges16 bronze badges

Try this

object1.content1 = object2;

answered Feb 17, 2017 at 4:38

Saurabh AgrawalSaurabh Agrawal

7,3262 gold badges23 silver badges47 bronze badges

Initialization

var object1 = {
   content1:"1"
}
var object2 = {
   content2:"2",
   content3:"3"
}

Put contents of object2 into object1's content1

object1.content1 = object2;//this is the code you are looking for


console.log[object2];

You are done!

answered Feb 17, 2017 at 4:43

codemirrorcodemirror

2,82627 silver badges41 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In order to push an array into the object in JavaScript, we need to utilize the push[] function. With the help of Array push function this task is so much easy to achieve. 

    push[] function: The array push[] function adds one or more values to the end of the array and returns the new length. This method changes the length of the array. But here we will use this function to push the whole array into an object.

    Syntax:  

    arr.push[element1[, ...[, elementN]]]

    An array can be inserted into the object with push[] function, below examples illustrate the above approach:

    Example 1:  

    Javascript

    var Obj = {             

        arrayOne: [],

        arrayTwo: []

    };

    var arraynew = ['Geeks', 'for', 'Geeks'];

    Obj.arrayOne.push[arraynew];     

    alert[Obj.arrayOne];

                         

    Output: 

    Example 2: 

    Javascript

    var Obj = {             

        arrayOne: ['Geeks', 'for', 'Geeks'],

        arrayTwo: []

    };

    var arraynew = ['Hello', 'World', '!!!']; 

    Obj['arrayTwo'].push[arraynew];     

    alert[Obj.arrayTwo];

                        

    Output: 
     

    Supported Browsers: The supported browser by the Array Push[] Function are listed below: 

    • Google Chrome 1.0
    • Internet Explorer 5.5
    • Mozilla Firefox 1.0
    • Safari
    • Opera

    JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


    Can we add object inside object in JavaScript?

    Use the Spread Operator to Append to Objects in JavaScript The spread operator is used to merge or clone objects in JavaScript. It can be used when all elements in an object need to be included in some list.

    How do you push an object in JavaScript?

    In order to push an array into the object in JavaScript, we need to utilize the push[] function. With the help of Array push function this task is so much easy to achieve. push[] function: The array push[] function adds one or more values to the end of the array and returns the new length.

    Can you push an object into an array JS?

    The push[] method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the end of the array.

    Can you nest objects in JavaScript?

    You can create nested objects within a nested object. In the following example, Salary is an object that resides inside the main object named Employee . The dot notation can access the property of nested objects.

    Chủ Đề