The array is as follows:
var item=[{name:' Tom ', Age:+, child:[{ name:' Jerry ', age :50 , child:[{ name:' William ', age :' } ' }, { name: ' Jessi ', age :+ }]
}];
The output httml is as follows:
1 <ol>2 <Li>Name:tom</Li>3 <Li>Age:70</Li>4 <Li>Children:5 <ol>6 <Li>Name:jerry</Li>7 <Li>Age:50</Li>8 <Li>Children:9 <ol>Ten <Li>Name:william</Li> One <Li>Age:20</Li> A </ol> - </Li> - </ol> the <ol> - <Li>Name:jessi</Li> - <Li>Age:30</Li> - </ol> + </Li> - </ol>
Thought analysis: The array is a nested array of arrays, iterating over the elements, when the current element has a child, a recursive call function that does not have a child when the,</ol> ends after closing the page element.
The JavaScript functions are implemented as follows:
1 var str= "";2 function Test (obj) {3 For (var key in obj) {4str+= "<ol>";5 if (obj[key].hasownproperty ("name")) {6Str=str+ "<Li>"+" Name: "+obj[key].name+"</Li>";7 }8 if (Obj[key].hasownproperty ("Age")) {9Str=str+ "<Li>"+" Age: "+obj[key].age+"</Li>";Ten } One if (!obj[key].hasownproperty ("Child")) { Astr+= "</ol>"; -str+= "</Li>"; -str+= "</ol>"; the } - if (Obj[key].hasownproperty ("Child")) { -Str=str+ "<Li>" +" Children: "; - test (obj[key].child); + } - } + } A test (item); atConsole.log ("%s", str);
Summary of front-end written questions-applying JavaScript functions to print arrays recursively on HTML pages