Iterate Nested Value in Array in JavaScript/ES6/ES7

Joseph

I need to iterate a nested value in my javascript.

My wanted output should be like this

shows: ['food.order', 'drink.order', 'play.basketball', 'play.soccer']

const results = [
  {
    "ID": "shops",
    "Shopping": [
      {
        "ID": "food.order",
        "Name": "Food"
      },
      {
        "ID": "drink.order",
        "Name": "Drink"
      }
    ]
  },
  {
    "ID": "fun",
    "Sports": [
      {
        "ID": "play.basketball",
        "Name": "Basketball"
      },
      {
        "ID": "play.soccer",
        "Name": "Soccer"
      },
    ]
  }
];

console.log(results);

const final = { shows: results.map(data => data['key'].ID) }
Sajeeb Ahamed

Your question is not clear though, but I am assuming that you are searching for ID property and want to grab the value of the ID and make an array. You can try this way-

const results = [{"ID": "shops", "Shopping": [{ "ID": "food.order", "Name": "Food"},{ "ID": "drink.order", "Name": "Drink"}]},{"ID": "fun", "Sports": [{ "ID": "play.basketball", "Name": "Basketball"},{ "ID": "play.soccer", "Name": "Soccer"}]}];

const ans = results.reduce((acc, item) => {

  // Iterate throw the each item's properties
  Object.values(item).forEach(val => {
    
      // Filter out the objects which has the `ID` property and get the value of the `ID`.
      const ids = typeof val === 'object' && val instanceof Array
        ? val.filter(x => x.ID !== undefined).map(({ID}) => ID)
        : [];

      acc = [...acc, ...ids];
    
  });

  return acc;
}, []);

console.log(ans);
.as-console-wrapper {min-height: 100%!important; top: 0}

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

iterate through list and return value from nested dict by position

分類Dev

Vue use v-for to iterate over an nested array of API response

分類Dev

filter array accoring to its nested array value

分類Dev

array doesn't iterate value just array name

分類Dev

how to iterate over a dictionary from value in an array and store in a new dictionary

分類Dev

In elasticsearch, how to group by value inside nested array

分類Dev

jq sort using the value of a nested array element

分類Dev

Mongo DB: Append a value to a nested array

分類Dev

get value from nested json array

分類Dev

Add key/value pair to hashtable (nested in an array, nested in a hashtable)

分類Dev

How to iterate in nested children with react?

分類Dev

Python iterate thru nested dictionary

分類Dev

How to iterate through nested classes?

分類Dev

Removing an associated key/value pair from array (nested)

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

how to get a particular object value from nested json array

分類Dev

is there a better way to test value exists in nested array using twig

分類Dev

Iterate in array of type struct

分類Dev

Iterate array in jQuery

分類Dev

Iterate array elements in <li>

分類Dev

Iterate array in array and echo strings

分類Dev

Iterate over array in v-for without knowing key-value pair beforehand

分類Dev

angular 7 *ngFor nested array iteration

分類Dev

How to iterate over and filter an array?

分類Dev

How to Iterate through an array in swift

分類Dev

iterate through array and store results

分類Dev

Iterate over each object In array

分類Dev

Iterate an array of arrays for a match from another array

分類Dev

Angular 7 nested for using parent value as index for child

Related 関連記事

  1. 1

    iterate through list and return value from nested dict by position

  2. 2

    Vue use v-for to iterate over an nested array of API response

  3. 3

    filter array accoring to its nested array value

  4. 4

    array doesn't iterate value just array name

  5. 5

    how to iterate over a dictionary from value in an array and store in a new dictionary

  6. 6

    In elasticsearch, how to group by value inside nested array

  7. 7

    jq sort using the value of a nested array element

  8. 8

    Mongo DB: Append a value to a nested array

  9. 9

    get value from nested json array

  10. 10

    Add key/value pair to hashtable (nested in an array, nested in a hashtable)

  11. 11

    How to iterate in nested children with react?

  12. 12

    Python iterate thru nested dictionary

  13. 13

    How to iterate through nested classes?

  14. 14

    Removing an associated key/value pair from array (nested)

  15. 15

    javascript return property value from nested array of objects based on condition

  16. 16

    how to get a particular object value from nested json array

  17. 17

    is there a better way to test value exists in nested array using twig

  18. 18

    Iterate in array of type struct

  19. 19

    Iterate array in jQuery

  20. 20

    Iterate array elements in <li>

  21. 21

    Iterate array in array and echo strings

  22. 22

    Iterate over array in v-for without knowing key-value pair beforehand

  23. 23

    angular 7 *ngFor nested array iteration

  24. 24

    How to iterate over and filter an array?

  25. 25

    How to Iterate through an array in swift

  26. 26

    iterate through array and store results

  27. 27

    Iterate over each object In array

  28. 28

    Iterate an array of arrays for a match from another array

  29. 29

    Angular 7 nested for using parent value as index for child

ホットタグ

アーカイブ