Need a recommendation on the compilation of the following algorithm:

There is a situation in which there may be a completely different JSON format. At one point in time, it can be:

{"root": { "child": "test_txt" } } 

At another point in time it may be:

 {"root": { "child_one": { "child_of_childOne": { "attr1":"test_txt" } }, "child_two": { "child_of_childTwo": { "attr2":"test_txt" } }, "child_three": { "child1_of_childThree": { "attr3":"test_txt" } "child2_of_childThree": { "attr4": { "the_deepest_attr": "test_txt" } } }, }; 

and even more difficult and even more nested may be at another point in time.

What is necessary? - you need to write a function that will iterate through all branches to the deepest attribute (to string "text_txt") and save the name of the deepest attribute with its value (as if you would need to get all attr from the example above)

Everything would be simple if there was only one branch, but there may be many branches with different levels of nesting. In addition, the names of the keys in this JSON are unknown, so you’ll not find much use for find.

My line of thought is this: take each object and recursively bypass it until I meet an attribute with a string. If an object has several "children" - remember it, saving it in an array. As you go around the "children" - remove them from the array.

But I'm not sure that this is the right approach, I decided to consult. Maybe someone has a ready-made solution. Thank you in advance.

  • About recursion - think correctly. It is not clear only about the array in which you add child and remove them from there, for what purpose is it done? (after all, they will be saved in the stack anyway, since the algorithm is recursive). Or do you need to store all the results found in this array? (then it is not clear why it may be necessary to remove elements from there). - Vladimir Gamalyan
  • To bypass the descendants of root , read the nesting level of each descendant with the memorization of depth, the key and the value of the deepest. If the next ones go deeper, change to new "leaders." No - go on. - user207618
  • @VladimirGamalian array I thought to use as operational. Those. So I went to root, I see that there are 3 elements - I took the first one, I remembered the rest, so that when I finish with the first one, go straight to the second, then the third. Otherwise, how can I understand that I have already bypassed the first element? Those. array planned to use as a map for future traversals. - Mr. Brightside
  • You can simply bypass all children in a loop, and for each, if it is an object, call the same function recursively, and if the line is to save the result. - Vladimir Gamalyan

1 answer 1

As an addition to the comment (only to show the concept):

 var j = { "root": { "child_one": { "child_of_childOne": { "attr1": "test_txt0" } }, "child_two": { "child_of_childTwo": { "attr2": "test_txt1" } }, "child_three": { "child1_of_childThree": { "attr3": "test_txt2" }, "child2_of_childThree": { "attr4": { "the_deepest_attr": "test_txt3" } } } } }; function traverse(o) { var i; for (var k in o) { i = o[k]; if (typeof i === 'string') { console.log(i); } else if (typeof i === 'object') { traverse(i); } } } traverse(j); 

  • It seems to fit =) I’ll check on those JSONs that I already have - Mr. Brightside