Home > Software engineering >  Take elements by key in objects array
Take elements by key in objects array

Time:11-03

Let's say I have got an array of objects like this:

const a = [
 0: {name: 'John', lastName: 'Smith'}
 1: {name: 'Juan', lastName: 'Perez'}
 myKey: true
 myKey2: false
]

How can I extract from my array just the value of 'myKey'? So the expected output would be

const myKeyValue = true

CodePudding user response:

If you want a single value and you know the key, use:

const myKeyValue = a.myKey
  • Related