How to generate an object with nested keys based on an array of arrays that hold it's key locations?

David Alsh

I have an array which contains arrays which describe the children inside an object as strings.

I am having trouble producing the desired result with the supplied input.

let obj = {}

let arr = [
  ["one", "two", "three", "four"],
  ["one", "two", "three", "four"],
  ["one", "two", "three", "four", "five"],
  ["one", "hi"]
]

console.log(JSON.stringify(obj, null, 4))

Desired output:

let result = {
  one: {
    children: {
      two : {
        children: {
          three: {
            children: {
              four: {
                children: {
                  five: {}
                }
              }
            }
          }
        }
      },
      hi: {}
    }
  }
}
CertainPerformance

One option would be to have a reduce nested inside another reduce, one iterating over the property arrays, and one iterating over each property.

You'll have to make sure you're checking the index of the property against the length of the property array, though, since you don't want to make children objects for the most deeply nested objects.

const arr = [
  ["one", "two", "three", "four"],
  ["one", "two", "three", "four"],
  ["one", "two", "three", "four", "five"],
  ["one", "hi"]
];
const output = arr.reduce((a, propArr) => {
  propArr.reduce((obj, prop, i) => {
    if (!obj[prop]) obj[prop] = {};
    if (!obj[prop].children && i !== propArr.length - 1) obj[prop].children = {};
    return obj[prop].children;
  }, a);
  return a;
}, {});
console.log(output);

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

How to Group JavaScript Array of Object based on key

分類Dev

How to group arrays in array by first element object key

分類Dev

How to sort nested Firebase DB's nested object array

分類Dev

Filter Array of object based in duplicate keys

分類Dev

What's the best way to merge to arrays based on key values in each array?

分類Dev

How to update existing nested object properties based on a list of key-value items

分類Dev

OrderBy in Angular.js for (key,value) object in reverse based on keys

分類Dev

Remove object based on a key from array of objects

分類Dev

Unset array based on it's key value

分類Dev

Need to form an array object with nested data out of 3 different arrays

分類Dev

How to modify an array's each object's properties based on another array in React/JS?

分類Dev

How to combine array to array multidimensional with object and key

分類Dev

Merge two arrays of objects but have the second array pushed into an object key

分類Dev

how to hold objects in array in hadoop

分類Dev

How to reference an Object based on a Key Value?

分類Dev

How to type function that converts array to object indexed by a key in array's elements in Typescript?

分類Dev

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

分類Dev

Deleting key to nested object?

分類Dev

Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

分類Dev

How to access the variables in a nested class with array object?

分類Dev

Turn object with (possible nested) arrays into an array of objects with single-item arrays

分類Dev

Convert array to object keys

分類Dev

How to write a nested array where the inner arrays are different lengths?

分類Dev

How to update key/value of a nested state object (React JS)

分類Dev

Consolidating an array of arrays into an object while merging values under a specific key (there are duplicates in the array)

分類Dev

How to convert square bracket object keys from URL location into nested object in Javascript?

分類Dev

Update Nested Object Array

分類Dev

Grouping a nested map based on keys of an inner map

分類Dev

How to make String variable hold value of 2 different arrays?

Related 関連記事

  1. 1

    How to Group JavaScript Array of Object based on key

  2. 2

    How to group arrays in array by first element object key

  3. 3

    How to sort nested Firebase DB's nested object array

  4. 4

    Filter Array of object based in duplicate keys

  5. 5

    What's the best way to merge to arrays based on key values in each array?

  6. 6

    How to update existing nested object properties based on a list of key-value items

  7. 7

    OrderBy in Angular.js for (key,value) object in reverse based on keys

  8. 8

    Remove object based on a key from array of objects

  9. 9

    Unset array based on it's key value

  10. 10

    Need to form an array object with nested data out of 3 different arrays

  11. 11

    How to modify an array's each object's properties based on another array in React/JS?

  12. 12

    How to combine array to array multidimensional with object and key

  13. 13

    Merge two arrays of objects but have the second array pushed into an object key

  14. 14

    how to hold objects in array in hadoop

  15. 15

    How to reference an Object based on a Key Value?

  16. 16

    How to type function that converts array to object indexed by a key in array's elements in Typescript?

  17. 17

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

  18. 18

    Deleting key to nested object?

  19. 19

    Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

  20. 20

    How to access the variables in a nested class with array object?

  21. 21

    Turn object with (possible nested) arrays into an array of objects with single-item arrays

  22. 22

    Convert array to object keys

  23. 23

    How to write a nested array where the inner arrays are different lengths?

  24. 24

    How to update key/value of a nested state object (React JS)

  25. 25

    Consolidating an array of arrays into an object while merging values under a specific key (there are duplicates in the array)

  26. 26

    How to convert square bracket object keys from URL location into nested object in Javascript?

  27. 27

    Update Nested Object Array

  28. 28

    Grouping a nested map based on keys of an inner map

  29. 29

    How to make String variable hold value of 2 different arrays?

ホットタグ

アーカイブ