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?
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.
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.
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.
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.
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'
}
}
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 have a couple of options:
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.
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"
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With