Hello! It does not work recursively to parse an array

{//Подается на вход "Name": "Level_1", "GUID": "fac21c7a-69e8", "Property": { "P": [{ "Name": "Level_2_1", "GUID": "fac21c98-69e8", "Property": { "P": [{ "Name": "Level_3_1", "GUID": "fac21ca6-69e8", "Property": {}, "Dir": false }, { "Name": "Level_3_2", "GUID": "fac21c9f-69e8", "Property": {}, "Dir": false }, { "Name": "Level_3_3", "GUID": "fac21ca2-69e8", "Property": {}, "Dir": false }] }, "Dir": true }, { "Name": "Level_2_2", "GUID": "fac21c81-69e8", "Property": { "P": [{ "Name": "Level_3_4", "GUID": "fac21c82-69e8", "Property": {}, "Dir": false }, { "Name": "Level_3_5", "GUID": "fac21c83-69e8", "Property": {}, "Dir": false }] }, "Dir": true }, { "Name": "Level_2_3", "GUID": "fac21cb0-69e8", "Property": { "P": { "Name": "Level_3_6", "GUID": "fac21cb1-69e8", "Property": {}, "Dir": false } }, "Dir": true }, { "Name": "Level_2_4", "GUID": "f71c31f0-9aab", "Property": { "P": { "Name": "Level_3_7", "GUID": "188b9a27-9aac", "Property": {}, "Dir": false } }, "Dir": true }] }, "Dir": true } function select(mass) { var div = $("#divN"); var newDiv = document.createElement('div'); if (typeof mass == 'object') { newmass.push(mass) } factorial(newmass) function factorial(mass) { mass.forEach(function (v) { if (v.dir == false) { return false; } else { var fguid = v.GUID.split("-")[0];// fguid = fguid.replace(/[^-0-9]/gim, ''); div.append('<div id="folder' + fguid + '" class="folder" onclick="toggle(' + fguid + ')">' + v.Name + '</div>'); if (v.Property.P.length > 0) { if (typeof v.Property.P == 'object') { for (var key in v.Property.P) { console.log('factorial ' + v.Property.P[key].Name) if (typeof v.Property.P == 'object' && v.Property.P.dir == false) { var m = [] m.push(v.Property.P) factorial(m); div.append('<div id="str' + fguid + '" class="str str' + fguid + '" onclick="emptyService(this.innerHTML)">' + v.Property.P[key].Name + '</div>'); } else { factorial(v.Property.P); div.append('<div id="str' + fguid + '" class="str str' + fguid + '" onclick="emptyService(this.innerHTML)">' + v.Property.P[key].Name + '</div>'); } } } else { v.Property.P.forEach(function (k) { factorial(k.Property.P); div.append('<div id="str' + fguid + '" class="str str' + fguid + '" onclick="emptyService(this.innerHTML)">' + k.Property.P.Name + '</div>'); }) } } } }) } } 

  • Snippets for code that can be run - Qwertiy
  • and what should this code do? - Grundy
  • should get a hierarchical list Name consisting of divs - full_null
  • Before the "Name": "Level_2_3" he parses normally, and then there is confusion and many repetitions - full_null
  • why the function is called factorial. and what a newmass, which is not declared anywhere? - Grundy

0