Answers for "how to add items to object in javascript"

5

adding element to javascript object

let person = {
  name : 'John Doe',
  age : 35
}

//Now we can add element by 2 ways
person.occupation = 'Web Designer'
//or
person['occupation'] = 'Web Designer'; //This is usefull for adding element within loop.

object[yourKey] = yourValue;
object.yourKey = yourValue;
Posted by: Guest on November-07-2020
13

how to add items to object in javascript

var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push(element);

// Array of Objects in form {element: {id: 10, quantity: 10} }
var element = {}, cart = [];
element.id = id;
element.quantity = quantity;
cart.push({element: element});
Posted by: Guest on March-13-2020

Code answers related to "how to add items to object in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language