1

I have an array of objects in a JSON format:

[
  {
    "word": "turkey"
  },
  {
    "word": "tiger"
  },
  {
    "word": "horse"
  },
  {
    "word": "pig"
  },
  {
    "word": "dog"
  },
  {
    "word": "cat"
  }
]

I want to extract the value for each "word" key and store it into an array like so:

let wordsArray = ["turkey", "tiger", "horse", "pig", "dog", "cat"];
  • [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+get+property+values+from+array+of+object) of [From an array of objects, extract value of a property as array](/q/19590865/4642212). – Sebastian Simon Aug 14 '21 at 20:21

2 Answers2

1

You just need to map the values:

console.log([
  {
    "word": "turkey"
  },
  {
    "word": "tiger"
  },
  {
    "word": "horse"
  },
  {
    "word": "pig"
  },
  {
    "word": "dog"
  },
  {
    "word": "cat"
  }
].map( ({word}) => word ))
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
1

You can use Array.map to extract the value of the word property:

const arr = [
  {
    "word": "turkey"
  },
  {
    "word": "tiger"
  },
  {
    "word": "horse"
  },
  {
    "word": "pig"
  },
  {
    "word": "dog"
  },
  {
    "word": "cat"
  }
]

const res = arr.map(e => e.word);
console.log(res);
Spectric
  • 30,714
  • 6
  • 20
  • 43