标签:复杂 console function head ret tor self doc 多层
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>复杂对象转数组</title> <script> var object = { "key1": { "name": 1, "age":10, "value": { "key11": { "name":11, "age": 110, "value": { "key111": { "name":111, "age":1110, "value":{} }, "key112": { "name":112, "age":1120, "value":{} }, } }, "key12": { "name":12, "age": 120, "value": { "key121": { "name":121, "age":1210, "value":{} } } } } }, "key2": { "name": 2, "age":20, "value": { "key22": { "name":22, "age": 220, "value": { "key222": { "name":222, "age":2220, "value":{} }, "key223": { "name":223, "age":2230, "value":{} } } } } }, "key3": { "name": 3, "age":30, "value":{ } } } console.log(object) var newArr = iterator_self(object, []); function iterator_self(obj, arr) { if(JSON.stringify(obj) != "{}") { for(var i in obj) { var oo = { "key": i, "name": obj[i].name,
"age": obj[i].age, "value": iterator_self(obj[i].value, []) } arr.push(oo); } } return arr; } console.log(newArr); </script> </head> <body> </body> </html>
标签:复杂 console function head ret tor self doc 多层
原文地址:https://www.cnblogs.com/monkey-cc/p/8811274.html