Skip to content
Advertisement

How to add dynamic keys with nested level from the array in javascript

Hi I want to create new object on the basis of path array. It will be dynamic. I tried with reduce but it is not giving correct result.

JavaScript

// this is what i want this object to be created programatically. after parsing above obj.

JavaScript

Advertisement

Answer

You can use simple for loops — reduce would also work (see further down), but I think the overhead of a callback is not worth it:

JavaScript

With reduce:

Using reduce it would translate to this:

JavaScript

With double reduce:

If the inner loop is also done through reduce, then:

JavaScript

The logical nullish assignment operator

If your environment has no support for ??= then use one of the following alternatives:

  • node[prop] ||= {}
  • (node[prop] = node[prop] ?? {})
  • (node[prop] = node[prop] || {})

Some comments on your code

As this function builds the object from scratch, it is not really necessary to treat intermediate versions of the object as immutable — as your code attempts to do at least in the case of path.length == 0: just keep extending the object through mutation.

return o[s] = {}; is destructive: if the property was already created from a previously processed path, then this will overwrite whatever was already assigned to o[s].

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement