I have grown fond of the LoDash / Underscore when writing larger projects.
Adding by obj['key'] or obj.key are all solid pure JavaScript answers. However both of LoDash and Underscore libraries do provide many additional convenient functions when working with Objects and Arrays in general.
Depending what you are looking for, there are two specific functions that may be nice to utilize and give functionality similar to the the feel of arr.push(). For more info check the docs, they have some great examples there.
In addition, it may be worthwhile mentioning jQuery.extend, it functions similar to _.merge and may be a better option if you already are using jQuery.
The second object will overwrite or add to the base object.
undefined values are not copied.
It may be worth mentioning the ES6/ ES2015 Object.assign, it functions similar to _.merge and may be the best option if you already are using an ES6/ES2015 polyfill like Babel if you want to polyfill yourself.
The second object will overwrite or add to the base object.
undefined will be copied.
var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
var injectObj = {isActive:true, timestamp:new Date()};
// function to inject key values in all object of json array
function injectKeyValueInArray (array, keyValues){
return new Promise((resolve, reject) => {
if (!array.length)
return resolve(array);
array.forEach((object) => {
for (let key in keyValues) {
object[key] = keyValues[key]
}
});
resolve(array);
})
};
//call function to inject json key value in all array object
injectKeyValueInArray(arrOfObj,injectObj).then((newArrOfObj)=>{
console.log(newArrOfObj);
});
There are two ways to add new properties to an object:
var obj = {
key1: value1,
key2: value2
};
Using dot notation:
obj.key3 = "value3";
Using square bracket notation:
obj["key3"] = "value3";
The first form is used when you know the name of the property. The second form is used when the name of the property is dynamically determined. Like in this example:
var getProperty = function (propertyName) {
return obj[propertyName];
};
getProperty("key1");
getProperty("key2");
getProperty("key3");
A real JavaScript array can be constructed using either:
I have grown fond of the LoDash / Underscore when writing larger projects.
Adding by
obj['key']
orobj.key
are all solid pure JavaScript answers. However both of LoDash and Underscore libraries do provide many additional convenient functions when working with Objects and Arrays in general..push()
is for Arrays, not for objects.Depending what you are looking for, there are two specific functions that may be nice to utilize and give functionality similar to the the feel of
arr.push()
. For more info check the docs, they have some great examples there._.merge (Lodash only)
The second object will overwrite or add to the base object.
undefined
values are not copied._.extend / _.assign
The second object will overwrite or add to the base object.
undefined
will be copied._.defaults
The second object contains defaults that will be added to base object if they don't exist.
undefined
values will be copied if key already exists.$.extend
In addition, it may be worthwhile mentioning jQuery.extend, it functions similar to _.merge and may be a better option if you already are using jQuery.
The second object will overwrite or add to the base object.
undefined
values are not copied.Object.assign()
It may be worth mentioning the ES6/ ES2015 Object.assign, it functions similar to _.merge and may be the best option if you already are using an ES6/ES2015 polyfill like Babel if you want to polyfill yourself.
The second object will overwrite or add to the base object.
undefined
will be copied.Best way to achieve same is stated below:
In order to prepend a key-value pair to an object so the for in works with that element first do this:
Output like this:-
There are two ways to add new properties to an object:
Using dot notation:
Using square bracket notation:
The first form is used when you know the name of the property. The second form is used when the name of the property is dynamically determined. Like in this example:
A real JavaScript array can be constructed using either:
The Array literal notation:
The Array constructor notation:
According to Property Accessors defined in ECMA-262(http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf, P67), there are two ways you can do to add properties to a exists object. All these two way, the Javascript engine will treat them the same.
The first way is to use dot notation:
But this way, you should use a IdentifierName after dot notation.
The second way is to use bracket notation:
and another form:
This way, you could use a Expression (include IdentifierName) in the bracket notation.