复杂对象转数组。多层嵌套,递归实现

Posted monkey-cc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复杂对象转数组。多层嵌套,递归实现相关的知识,希望对你有一定的参考价值。

<!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>

 

以上是关于复杂对象转数组。多层嵌套,递归实现的主要内容,如果未能解决你的问题,请参考以下文章

java怎么解析多层嵌套的json得到每个元素

Mongodb多层嵌套数组如何精确查询(内容处为格式化的json截图,评论里面是json的内容)?

iview 自定义验证表单多层嵌套如何实现

对象遍历,多层嵌套数组,for in方法对象遍历,map方法数组遍历

C# json提取多层嵌套到数组

前端片段整理