How can I append an object to another object?


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?

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.


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

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"
    }
]

There are two ways but the first one is the best way you can retrieve data easily using for loop

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

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

1) create one array variable and push all your objects in that array

var myObjectList = [];
myObjectList.push(object1);
myObjectList.push(object2);

2) You can create the objects and assign value to that object

var myObjectList = {object1: object1, object2 : object2};

I vote up CaptainAdmin answer, of using spread syntax. Very useful, and good to know it is there. However, in this case, it'd be both more convenient and correct to use something like:

var o1 = {
    product_name: "Super One",
};

var o2 = {
    quantity: 50,
};

specializeWith(o1, o2);

Where:

function specialize_with(o, S) { for (var prop in S) { o[prop] = S[prop]; } }

Then your o1 will be:

var o1 = {
    product_name: "Super One",
    quantity: 50,
};

Unfortunately, looking closely at the original question, I see that's not what was asked in the first place. The array of objects was needed, not an inheritance or specialization or extension. Still, leaving it here.

See also https://github.com/latitov/OOP_MI_Ct_oPlus_in_JS, the specializeWith() is from there.

Again, please look at https://github.com/latitov/OOP_MI_Ct_oPlus_in_JS.