javascript object access key code example

Example 1: for key value in object javascript

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

Example 2: js get object keys

myObject = {
	"key": "value"
}

Object.keys(myObject); // get array of keys

Example 3: get all keys of object in javascript

myObject = {
	"key": "value",
    "key2":"value2"
}
Object.keys(myObject);
//console.log(Object.keys(myObject))   = ["key", "key2"]

Example 4: js create object with properties

var mycar = new Car('Eagle', 'Talon TSi', 1993);

Example 5: js create object with properties

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Tags:

Misc Example