//by using recursive forEach () times within the array of data The function getID (product id) { Var o={} Product. The forEach (function (item) { If (item id===id) { O=item; The console. The log (o) } else if (item. Goods & amp; & Item. Goods. Length & gt; 0 { GetID (item. Goods, id) } }) return o; }
The console. The log (getID (data, 121))
The above code snippet, the first log normal output, but why the second log output is an empty object, is not normal for getID (item) goods, id) of the item after the recursive traversal to conform to the objects stored in the o the hollow object, how to print out a {}? (the output shown in the figure below)
CodePudding user response:
Because you're operating object o defined within the function, and then every time you getID will be reset to its {}
CodePudding user response:
Reset to zero after the assignment again, isn't it [align=center]
If (item id===id) { O=item; The console. The log (o) } else if (item. Goods & amp; & Item. Goods. Length & gt; 0 { GetID (item. Goods, id)