JavaScript Objects

up:: JavaScript

Methods

  • delete object.key;
  • users.hasOwnProperty(Alan) is the same as 'Alan' in users

Basics

var myObj = {
  "SpaceName": "Kirk",
  "More Space": "Spock",
  "NoSpace": "USS Enterprise"
};
// Objects can be accessed via dot notation:
S;
 
// Objects with spaces need to be accessed by bracket notation:
myObj["More Space"];
 
// Objects can be deleted:
delete myObj.SpaceName;
 
// Check if property exists
myObj.hasOwnProperty(SpaceName);

Constructors and Prototypes

Snippets

Find objects based on value in an array of objects

allCars: [
{
brand: "Honda",
color: "red"
},
{
brand: "Tesla",
color: "white"
}
];
 
const car = allCars.find(entry => entry.color === "red");