Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append an object to another object?

Tags:

javascript

Can someone help me append an object another object? For example, I have the original object:

var object = {
    { product_code: '55993',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '300.00',
      quantity: '3' 
    }
}

and I want to append:

    { product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }

So in the end, object looks like:

object = {
    { product_code: '55993',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '300.00',
      quantity: '3' 
    },
    { product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }
}

How do I do that?

like image 950
Trung Tran Avatar asked Feb 19 '16 02:02

Trung Tran


People also ask

Does append create a new object?

append() will place new items in the available space. Lists are sequences that can hold different data types and Python objects, so you can use . append() to add any object to a given list. In this example, you first add an integer number, then a string, and finally a floating-point number.

How do you append to an array of objects?

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.

How do you conditionally add an object?

To conditionally add a property to an object, we can make use of the && operator. In the example above, in the first property definition on obj , the first expression ( trueCondition ) is true/truthy, so the second expression is returned, and then spread into the object.

How do I combine two JavaScript objects?

To merge two objects in JavaScript, you can use the spread ... operator. The spread operator creates a new object with all the properties from the first and second object. If there's two properties with the same name, the property from the second object wins out.


3 Answers

You can use ES6 spread operator to append object.

object = { ...object, 
...{ product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }
}
like image 129
CaptainZero Avatar answered Sep 22 '22 10:09

CaptainZero


Your object variable should be an array, not an object:

    var object = [
        { product_code: '55993',
          brand: 'Phillips66',
          category: 'LUBES AND GREASES',
          description: 'cuatro cinco seis',
          price: '300.00',
          quantity: '3' 
        }
    ]

Then you can add other objects into it:

object.push({ product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    });

Hope it helps.

like image 44
Lean Avatar answered Sep 22 '22 10:09

Lean


You have a couple of options:

  1. Define a property name under which to store the second object, which won't give you a structure like the one you've shown at the end of your question.

  2. Use an array, which will give you something very nearly like what you have at the end of your question:

    [
        { product_code: '55993',
          brand: 'Phillips66',
          category: 'LUBES AND GREASES',
          description: 'cuatro cinco seis',
          price: '300.00',
          quantity: '3' 
        },
        { product_code: '90210',
          brand: 'Phillips66',
          category: 'LUBES AND GREASES',
          description: 'cuatro cinco seis',
          price: '500.00',
          quantity: '5' 
        }
    ]
    

    Note the [ and ] instead of { and } at the outermost level.

If you have an object:

var object1 = {foo: "bar"};

and another object:

var object2 = {biz: "baz"};

Then option #1 looks like this:

object1.anyNameHere = object2;

...which will give you:

{
   foo: "bar",
   anyNameHere: {
       biz: "baz"
   }
}

Or option #2:

var a = [object1, object2];

...which will give you:

[
    {
        foo: "bar"
    },
    {
        biz: "baz"
    }
]
like image 31
T.J. Crowder Avatar answered Sep 19 '22 10:09

T.J. Crowder