0

I am trying to convert Array into object

For example:

I want to convert, below array into

var a = ['a','b','c','d']

Below object

var obj = {
  'a': {
    'b': {
      'c': {
        'd': 'any value'
      }
    }
  }
}

Any one have any idea ?

6
  • Does this answer your question? Convert Array to Object Commented Apr 7, 2021 at 3:41
  • 1
    No this is not my question Commented Apr 7, 2021 at 3:45
  • 1
    The question should be reopened, Object.assign, spread, or similar in the duplicate question won't nest the properties. ['a','b','c','d'].reverse().reduce((acc, curr) => { if (acc) return { [curr]: { ...acc } }; return { [curr]: 'any value' }; }, null); Commented Apr 7, 2021 at 4:04
  • 1
    @AlexanderStaroselsky the link posted by Shreyas isn't the one used to close this question. See the link at the very top. The answer there does indeed nest the properties Commented Apr 7, 2021 at 4:10
  • 1
    @Phil You're absolutely right, missed that link, that solution would definitely solve it and is cleaner. Commented Apr 7, 2021 at 4:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.