Ati
Ati

Reputation: 304

How to get name property from array of objects arrays?

I have array, which contains arrays of objects from different tables. How can I get all names of all these objects in array of arrays? I know, they are weirdly nested, but that's how structure is for now. enter image description here

Upvotes: 1

Views: 1275

Answers (2)

M.Hassan Nasir
M.Hassan Nasir

Reputation: 851

use a nested for loop on the array

var names = []
for (let i = 0 ; i < array.length ; i++){
    for(let j = 0 ; j<array[i].length; j ++){
        names.push(array[i][j].name)
    }
}
console.log(names)

Upvotes: 0

DoneDeal0
DoneDeal0

Reputation: 6257

Just write:

const getNames = (data) => data.flat().map(({name})=> name)

Upvotes: 1

Related Questions