现在有一个json  需要存入  数组   jsonarry  怎样弄?{
"data": [
{
"typeid": 2,
"txt": "人力资源",
"title": "",
"children": [
{
"nameid": 6,
"txt": "入职手续",
"code": "crylbx",
"title": "身份证、正反面等入职"
},{
"nameid": 7,
"txt": "社保接入",
"code": "crylbx",
"title": "社保卡号、社保密码社保接入"
}
]
}, {
"typeid": 1,
"txt": "计划生育",
"title": "",
"children": [
{
"nameid": 1,
"txt": "准生证",
"code": "zsz",
"title": "准生证准备材料"
}, {
"nameid": 2,
"txt": "独生子女证",
"code": "dszn",
"title": "独生子女证  准备材料"
}, {
"nameid": 3,
"txt": "计划生育证",
"code": "jhsy",
"title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
}
]
}
]
}
上边是 json  
下边这个怎么写  或者  哪位大神 有更好的方法
var jsonarry=[ ];
$.each(data.data, function(i, item) {
jsonarry.push({
//将json 追加入数组
name: this.name,
txt: this.txt,
children: 这里怎么写
});

});

解决方案 »

  1.   

    javascript树数据源嵌套结构和扁平结构互转
      

  2.   

    如果只单纯的把json装入数组的话,如下可以实现。
    let jsonData = {
        "data": [
            {
            "typeid": 2,
            "txt": "人力资源",
            "title": "",
            "children": [
                {
                    "nameid": 6,
                    "txt": "入职手续",
                    "code": "crylbx",
                    "title": "身份证、正反面等入职"
                },{
                    "nameid": 7,
                    "txt": "社保接入",
                    "code": "crylbx",
                    "title": "社保卡号、社保密码社保接入"
                }
            ]
            }, 
            {
            "typeid": 1,
            "txt": "计划生育",
            "title": "",
            "children": [
                {
                    "nameid": 1,
                    "txt": "准生证",
                    "code": "zsz",
                    "title": "准生证准备材料"
                }, {
                    "nameid": 2,
                    "txt": "独生子女证",
                    "code": "dszn",
                    "title": "独生子女证  准备材料"
                }, {
                    "nameid": 3,
                    "txt": "计划生育证",
                    "code": "jhsy",
                    "title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
                }
            ]
            }
        ]
    };
    let jsonArr=[ ];
    for(let i in jsonData){
        jsonArr.push({
            i: jsonData[i]
        })
    }
    console.log(jsonArr);
      

  3.   


    <script>
    var jsonarry = new Array();
    var cArray = new Array();
    cArray.push(new child("me", "test", []));
    cArray.push(new child("you", "test2", []));jsonarry.push({
    name: "",
    txt: "",
    children: cArray
    });alert(jsonarry[0].children[0].name);
    alert(jsonarry[0].children[1].name);function child(name, txt, children){
    this.name = name;
    this.txt = txt;
    this.children = children;
    }
    </script>
      

  4.   

    ES6
    let arr = Array.from(json)搞定
      

  5.   

    var jsonData = {
        "data": [
            {
            "typeid": 2,
            "txt": "人力资源",
            "title": "",
            "children": [
                {
                    "nameid": 6,
                    "txt": "入职手续",
                    "code": "crylbx",
                    "title": "身份证、正反面等入职"
                },{
                    "nameid": 7,
                    "txt": "社保接入",
                    "code": "crylbx",
                    "title": "社保卡号、社保密码社保接入"
                }
            ]
            }, 
            {
            "typeid": 1,
            "txt": "计划生育",
            "title": "",
            "children": [
                {
                    "nameid": 1,
                    "txt": "准生证",
                    "code": "zsz",
                    "title": "准生证准备材料"
                }, {
                    "nameid": 2,
                    "txt": "独生子女证",
                    "code": "dszn",
                    "title": "独生子女证  准备材料"
                }, {
                    "nameid": 3,
                    "txt": "计划生育证",
                    "code": "jhsy",
                    "title": "计划生育证 准备材料\r\n1、材料1\r\n2、材料3\r\n3、材料3"
                }
            ]
            }
        ]
    };
    var jsonArr=[ ];
    for(var i in jsonData.data){
        jsonArr.push({
            typeid: jsonData.data[i].typeid,//json里面没有name,故写了typeid
    txt: jsonData.data[i].txt,
    children: jsonData.data[i].children
    });}
    console.log(jsonArr); 
      

  6.   

    这可能是代码最少的办法了const { data } = obj
    const arr = data.map(el => el)
    console.log(arr)结果[ { typeid: 2,
        txt: '人力资源',
        title: '',
        children: [ [Object], [Object] ] },
      { typeid: 1,
        txt: '计划生育',
        title: '',
        children: [ [Object], [Object], [Object] ] } ]