Changing “key” and “values” into “name” and “child

2020-02-15 03:16发布

问题:

UPDATE: Solved thanks To @AmeliaBR, that answered my specific question here after I asked it.

  • This fiddle shows the adaptation of the solution to my specific problem.


My question was:

  • I'm looking for something like this without changing the structure of the functions I want to use.

  • This works great if you have defined parents inside the structure, but I can't get access to it.

What I ideally would do is to also do it work efficiently, and I think that in order to do so nest() might be a good answer.

I want to change

"key": "keyname", "values":"a value" 

into

"name": "keyname", "children":"a value"

And in the leaf node change (if possible making the rest of useless data dissapear)

 "value": "a value" 

into

"size": "a value"

I got a bit sad when I read this:

"I don't see any other method than going trough all elements recursively and changing names, which requires copying and deleting each field. However you can always use the source code of nest() as inspiration."

Can you beat that guy with your knowledge?