JavaScript Object Methods

This YouTube video was created by Steve Griffith.

Objects play such an important part of the JavaScript. Not only is nearly every component of JavaScript an extension of the Object class, but objects can also be an excellent way of storing an retrieving data.

However, unlike arrays, object literals, the object created with using a set of curly braces, are not iterable and do not have properties like length. Fortunately, JavaScript does include methods that allow for the properties and values of an object literal to be returned as an iterable object.

Object.keys

The Object.keys() method returns an array of the property names of the specified object.

const person = {
  firstName: 'John',
  lastName: 'Smith',
  email: 'jsmith@email.com'
}

const keys = Object.keys(person)
console.log(keys) // ['firstName', 'lastName', 'email']

Object.values

The Object.values() method returns an array of the values of the specified object.

const person = {
  firstName: 'John',
  lastName: 'Smith',
  email: 'jsmith@email.com'
}

const values = Object.values(person)
console.log(values) // ['John', 'Smith', 'jsmith@email.com']

Object.entries

The Object.entries() method returns an array of arrays, with each array containing a key / value pair of the specified object.

const person = {
  firstName: 'John',
  lastName: 'Smith',
  email: 'jsmith@email.com'
}

const entries = Object.entries(person)
console.log(entries) // [['firstName', 'John'], ['lastName', 'Smith'], ['email', 'jsmith@email.com']]