Javascript Map objects

It’s possible to convert the nested map to a nested plain object with a recursive function (code at the end of this post), but as you say, there’s no point in doing this if you just want the output of d3.nest.

Here’s a question: is it possible that the code that requires a nested object would somehow benefit from using nested maps instead?

function mapToObj(map) {
  if (map instanceof Map) {
    return Array.from(map).map(([key, val]) => ({name:key, children:mapToObj(val)}));
  }
  return map;
}

Edit: that doesn’t wrap the outermost layer, so use it like this, I guess:

dat = ({name:"moluscs", children:mapToObj(mapped)})
2 Likes