add object code example
Example 1: append object to object javascript
const originalObj = { name: 'John', age: 34 }
let newObj = { ...originalObj, city: 'New York' }
newObj = { ...newObj, language: 'en' }
Example 2: ad data to js object
var myObject = {
sProp: 'some string value',
numProp: 2,
bProp: false
};
myObject.prop2 = 'data here';
myObject.sProp = 'A new string value for our original string property';
alert(myObject.sProp)
var val = myObject.sProp;
Example 3: adding element to javascript object
let person = {
name : 'John Doe',
age : 35
}
person.occupation = 'Web Designer'
person['occupation'] = 'Web Designer';
object[yourKey] = yourValue;
object.yourKey = yourValue;
Example 4: javascript add to object
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
Example 5: how to a property from a JavaScript object
delete myObject.regex;
delete myObject['regex'];
var prop = "regex";
delete myObject[prop];
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject.regex;
console.log(myObject);
Example 6: Add New Properties to a JavaScript Object
var myDog = {
"name": "Happy Coder",
"legs": 4,
"tails": 1,
"friends": ["freeCodeCamp Campers"]
};
myDog.bark = "woof";
myDog["bark"] = "woof";