How to make an array of multiple nested objects?

Squanchy

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?

Jack Bashford

Just use Object.values and flat - this works even if you don't know the key of the nested object:

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

const res = Object.values(Object.values(Filus)[0]).flat();
console.log(res);

ES5 syntax:

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

var res = Object.keys(Filus[Object.keys(Filus)[0]]).map(function(key) { 
  return Filus[Object.keys(Filus)[0]][key];
}).reduce(function(acc, curr) {
  return acc.concat(curr);
});

console.log(res);


It's also easy if you have the key:

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

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

ES5 syntax:

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

var res = Object.keys(Filus.male).map(function(key) { 
  return Filus.male[key];
}).reduce(function(acc, curr) {
  return acc.concat(curr);
});

console.log(res);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to query nested array of objects by also filtering nested array of nested array in mongo

分類Dev

How to make array of inherited objects - Java

分類Dev

How to make IBOutlets out of an array of objects?

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Search nested array of objects

分類Dev

How to parse multiple nested JSON objects/arrays with GSON?

分類Dev

How to use reduce to retrieve values from deep nested array objects

分類Dev

Flattening deeply nested array of objects

分類Dev

Vue access nested array objects

分類Dev

Destruct nested array of objects javascript

分類Dev

How to flat array of objects which can contain nested objects in my case

分類Dev

Convert array of objects with nested array of objects into array-like object

分類Dev

Regroup array of objects based on nested array

分類Dev

How to get a the sum of multiple arrays within an array of objects?

分類Dev

How to have an array with multiple types of objects, or a function, in C

分類Dev

How to loop over objects nested in objects in TypeScript

分類Dev

How to order by nested objects fields?

分類Dev

Sort by multiple objects in a array in Javascript

分類Dev

why map() is mutating original objects in a nested array?

分類Dev

Unable to convert a nested object into an array of objects and viceversa

分類Dev

Filtering deeply nested array of objects in Mongo DB

分類Dev

Angular: Nested ngFor on array of objects with FormArray

分類Dev

Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?

分類Dev

How to make it a row in a nested loop

分類Dev

C++ | How to make a vector which contains multiple objects of different classes

分類Dev

Make json from array of objects javascript

分類Dev

How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

分類Dev

how would you make it so that it grabs a random number and uses it to grab objects from an array

分類Dev

How to take a value from nested array(json format) without using multiple foreach in php

Related 関連記事

  1. 1

    how to query nested array of objects by also filtering nested array of nested array in mongo

  2. 2

    How to make array of inherited objects - Java

  3. 3

    How to make IBOutlets out of an array of objects?

  4. 4

    How to get the combination of array values from nested arrays in an array of objects

  5. 5

    Search nested array of objects

  6. 6

    How to parse multiple nested JSON objects/arrays with GSON?

  7. 7

    How to use reduce to retrieve values from deep nested array objects

  8. 8

    Flattening deeply nested array of objects

  9. 9

    Vue access nested array objects

  10. 10

    Destruct nested array of objects javascript

  11. 11

    How to flat array of objects which can contain nested objects in my case

  12. 12

    Convert array of objects with nested array of objects into array-like object

  13. 13

    Regroup array of objects based on nested array

  14. 14

    How to get a the sum of multiple arrays within an array of objects?

  15. 15

    How to have an array with multiple types of objects, or a function, in C

  16. 16

    How to loop over objects nested in objects in TypeScript

  17. 17

    How to order by nested objects fields?

  18. 18

    Sort by multiple objects in a array in Javascript

  19. 19

    why map() is mutating original objects in a nested array?

  20. 20

    Unable to convert a nested object into an array of objects and viceversa

  21. 21

    Filtering deeply nested array of objects in Mongo DB

  22. 22

    Angular: Nested ngFor on array of objects with FormArray

  23. 23

    Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?

  24. 24

    How to make it a row in a nested loop

  25. 25

    C++ | How to make a vector which contains multiple objects of different classes

  26. 26

    Make json from array of objects javascript

  27. 27

    How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

  28. 28

    how would you make it so that it grabs a random number and uses it to grab objects from an array

  29. 29

    How to take a value from nested array(json format) without using multiple foreach in php

ホットタグ

アーカイブ