0

I am new to Javscript and , I have a response from the server like this,

[
    {
        "name": "jim",
        "color": "blue",
        "age": "22"
    },
    {
        "name": "Sam",
        "color": "blue",
        "age": "33"
    },
    ... 80 more.
]

There are 85 such objects in the responseArray, How do I use reduce method to get a resultArray with all the ages in the array. What I intend to make is an ageArray, as below:

const ageArray = [22,33,77 ... 88 more]

How to go about?

Vegeta
  • 109
  • 2
  • 7
  • 1
    Why do you need the ageArray? It looks need you want to do some calculation later...If this is the case, you may need ```reduce()``` actually. – ikhvjs Jun 08 '21 at 15:06
  • Using reduce(), you can try like this const ageArray = a.reduce((x, y) => { x.push(y.age); return x;}, []); console.log(ageArray ); – Ashok Kumar Jun 09 '21 at 06:42

1 Answers1

0

Use .map:

let ageArray = arr.map(a => a.age);
tymeJV
  • 103,943
  • 14
  • 161
  • 157