Creating a nested Javascript Object from an array of Strings

Abdullah Yahya

What's the best way to create a N levels nested object (where N is the size of the array) for example:

const arr = ['a','b','c','d']

The output object should look like this:

{
  a: {
    b: {
      c: {
        d: true
      }
    }
  }
}
Avanthika

You can use array.reduce, it helps you pass an accumulator where you can accumulate your nested obj.

const array = ['a','b','c','d'];
const object = {};
array.reduce((o, s) => { 
  return o[s] = {};
}, object);
console.log(object);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

creating a object from array of strings

From Dev

How to get array of strings from nested object

From Dev

Loop for creating hrefs from strings in javascript array

From Dev

Simple array object from nested object in javascript

From Dev

Creating javascript object from array values

From Dev

Creating a List Object from Array in Javascript

From Dev

Creating HTML list from Javascript object array

From Dev

Create nested object from object with multiple strings, javascript

From Dev

Infer union of strings from an array of objects, while creating an object

From Dev

Create nested object from array javascript

From Dev

json object from javascript nested array

From Dev

Create nested object from array of objects in JavaScript

From Dev

Create a nested object from Array javascript

From Dev

Creating an updated and edited nested object from given one (JavaScript)

From Dev

Is there a way to extract nested types from object, based on strings in array in typescript

From Dev

JavaScript - creating a comma delimited string from a lookup in a heavily nested array

From Dev

Creating a nested object from entries

From Dev

Nested object in Javascript array

From Dev

Javascript - nested dictionary from array of strings separated with dots

From Dev

Creating a UL from nested array

From Dev

Creating an array from a nested dictionary

From Dev

Javascript creating arrays from strings

From Dev

Creating nested hashes from nested array

From Dev

Creating nested objects from a flat array of strings based on a number of specific characters

From Dev

From nested array to array of object

From Dev

Creating a Javascript object array on the fly

From Dev

JSONATA: Creating Array from Nested Array of Objects

From Dev

Javascript: How to make array from object with key value pairs as strings?

From Dev

How to find maxium value from nested array Object in Javascript?

Related Related

  1. 1

    creating a object from array of strings

  2. 2

    How to get array of strings from nested object

  3. 3

    Loop for creating hrefs from strings in javascript array

  4. 4

    Simple array object from nested object in javascript

  5. 5

    Creating javascript object from array values

  6. 6

    Creating a List Object from Array in Javascript

  7. 7

    Creating HTML list from Javascript object array

  8. 8

    Create nested object from object with multiple strings, javascript

  9. 9

    Infer union of strings from an array of objects, while creating an object

  10. 10

    Create nested object from array javascript

  11. 11

    json object from javascript nested array

  12. 12

    Create nested object from array of objects in JavaScript

  13. 13

    Create a nested object from Array javascript

  14. 14

    Creating an updated and edited nested object from given one (JavaScript)

  15. 15

    Is there a way to extract nested types from object, based on strings in array in typescript

  16. 16

    JavaScript - creating a comma delimited string from a lookup in a heavily nested array

  17. 17

    Creating a nested object from entries

  18. 18

    Nested object in Javascript array

  19. 19

    Javascript - nested dictionary from array of strings separated with dots

  20. 20

    Creating a UL from nested array

  21. 21

    Creating an array from a nested dictionary

  22. 22

    Javascript creating arrays from strings

  23. 23

    Creating nested hashes from nested array

  24. 24

    Creating nested objects from a flat array of strings based on a number of specific characters

  25. 25

    From nested array to array of object

  26. 26

    Creating a Javascript object array on the fly

  27. 27

    JSONATA: Creating Array from Nested Array of Objects

  28. 28

    Javascript: How to make array from object with key value pairs as strings?

  29. 29

    How to find maxium value from nested array Object in Javascript?

HotTag

Archive