Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an array of multiple nested objects?

I have such an object.

let Filus = {
     male: {
    hat: [1],
    jacket: [2],
    pants: [3],
    shoes: [4],
    suit: [5]
  }
};

I want to get this array from this object.

let Filus = [1,2,3,4,5];

How to do it?

like image 771
Silicum Silium Avatar asked Sep 03 '26 03:09

Silicum Silium


1 Answers

You can get values of nested object male using Object.values() and then use flat()

let Filus = { male : { hat: [1], jacket: [2], pants: [3], shoes: [4], suit: [5] } };

const res = Object.values(Filus.male).flat();
console.log(res)

You can also do that without flat() using concat() and spread operator.

let Filus = { male : { hat: [1], jacket: [2], pants: [3], shoes: [4], suit: [5] } };

const res = [].concat(...Object.values(Filus.male));
console.log(res)
like image 198
Maheer Ali Avatar answered Sep 05 '26 17:09

Maheer Ali