-1

I am trying to convert the "count" key from each object within the test array into a new array so I end up with something like

newCount =[0,0,0,0]

const test =  [
      {
        id: 0,
        count: 0,
        image: "",
        text: 'Some text about finn'
      },
      {
        id: 1,
        count: 0,
        image: "",
        text: 'Some text about daphne'
      },
      {
        id: 2,
        count: 0,
        image: "",
        text: 'Some text  finn'
      },
      {
        id: 3,
        count: 0,
        image: "",
        text: 'Some text  daphne'
      }
    ]

2 Answers2

2

You can use Array.prototype.map() on your test array to extract the count property value of each object:

const newCount = test.map(t => t.count);

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
0

You can use a map on the array:

const newarray = test.map(item => item.count);

Array map documentation

Peter Halligan
  • 662
  • 5
  • 14