Object keys() method

Find out all the information about the JavaScript keys() method of the Object object

Object.keys()Accepts an object as a parameter and returns an array of all its (own) enumerable properties.

const car = {
  color: 'Blue',
  brand: 'Ford',
  model: 'Fiesta'
}

Object.keys(car) //[ ‘color’, ‘brand’, ‘model’ ]

I said countless attributes. This means that their internal enumerable flag is set to true, which is the default setting.Check MDNMore information on this topic.

One useObject.keysThe function is to create a copy of an object with all properties, except for the following properties:

const car = {
  color: 'blue',
  brand: 'Ford'
}
const prop = 'color'

const newCar = Object.keys(car).reduce((object, key) => { if (key !== prop) { object[key] = car[key] } return object }, {})

Download mine for freeJavaScript beginner's manual


More js tutorials: