javascript - Comment utiliser JS pour convertir des tableaux unidimensionnels et imbriqués d'objets JSON?
習慣沉默
習慣沉默 2017-05-19 10:13:06
0
3
670

Comment convertir ces deux formats de données l'un dans l'autre via JS ?
Le même contenu, les plats sont combinés en formats imbriqués et les imbriqués sont désassemblés en formats plats
Je n'ai pas pu comprendre après un long moment, aidez-moi s'il vous plaît !

[
    {
        "id":1,
        "number":"100.000",
        "name": "admin.",
        "level":1,
        "children": [
            {
                "id": 2,
                "number": "100.210",
                "name": "admin.marketing",
                "level": 2
            },
            {
                "id": 3,
                "number": "100.260",
                "name": "admin.operation",
                "level": 2,
                "children": [
                    {
                        "id": 5,
                        "number": "260.261",
                        "name": "operation.content",
                        "level": 3
                    },
                    {
                        "id": 6,
                        "number": "260.262",
                        "name": "operation.promote",
                        "level": 2
                    },
                    {
                        "id": 7,
                        "number": "260.263",
                        "name": "operation.service",
                        "level": 2
                    }
                ]
            },
            {
                "id": 4,
                "number": "100.280",
                "name": "admin.development",
                "level": 2
            }
        ]
    }
]
[
  {
      "id":1,
      "number":"100.000",
      "name": "admin.",
      "level":1
  },
  {
      "id": 2,
      "number": "100.210",
      "name": "admin.marketing",
      "level": 2
  },
  {
      "id": 3,
      "number": "100.260",
      "name": "admin.operation",
      "level": 2
  },
  {
      "id": 4,
      "number": "100.280",
      "name": "admin.development",
      "level": 2
  },
    {
        "id": 5,
        "number": "260.261",
        "name": "operation.content",
        "level": 3
    },
    {
        "id": 6,
        "number": "260.262",
        "name": "operation.promote",
        "level": 3
    },
    {
        "id": 7,
        "number": "260.263",
        "name": "operation.service",
        "level": 3
    }
]
習慣沉默
習慣沉默

répondre à tous(3)
滿天的星座

Utiliser la récursivité pour implémenter

// 待处理数值 
var data = [
    {
        "id":1,
        "number":"100.000",
        "name": "admin.",
        "level":1,
        "children": [
            {
                "id": 2,
                "number": "100.210",
                "name": "admin.marketing",
                "level": 2
            },
            {
                "id": 3,
                "number": "100.260",
                "name": "admin.operation",
                "level": 2,
                "children": [
                    {
                        "id": 5,
                        "number": "260.261",
                        "name": "operation.content",
                        "level": 3
                    },
                    {
                        "id": 6,
                        "number": "260.262",
                        "name": "operation.promote",
                        "level": 2
                    },
                    {
                        "id": 7,
                        "number": "260.263",
                        "name": "operation.service",
                        "level": 2
                    }
                ]
            },
            {
                "id": 4,
                "number": "100.280",
                "name": "admin.development",
                "level": 2
            }
        ]
    }
]

// 递归函数
function headFor(o, cb){
    if (!o) return; 

    o.forEach(child => {
        cb(child); 
        if (child.children) {
            headFor(child.children, cb);    
        }
    });
}

// 遍历 data 的结果保存在 res 
var res = []; 

// 调用 递归函数headFor  (注意 第二个参数是函数)
headFor(data, item => {
    var temp = {}; 
    temp.id = item.id
    temp.number = item.number;
    temp.name = item.name; 
    temp.level = item.level; 
    res.push(temp); 
}); 

// 告诉数组怎么判断两个元素 然后让他数组自己去干 
res.sort((a, b) => {
    return a.id > b.id; 
}); 

// 打印 
res.forEach(e => console.log(e)); 
console.log(JSON.stringify(res)); 

Les données les plus externes sont un tableau. En fait, elles peuvent être considérées comme l'attribut enfant externe, et les éléments enfants des données peuvent eux-mêmes avoir des enfants.

巴扎黑

Maintenant, la méthode inverse a été complétée avec l'aide de l'ami du groupe hibou !

    var data=[
        {
            "id":1,
            "number":"100.000",
            "name": "admin.",
            "level":1
        },
        {
            "id": 2,
            "number": "100.210",
            "name": "admin.marketing",
            "level": 2
        },
        {
            "id": 3,
            "number": "100.260",
            "name": "admin.operation",
            "level": 2
        },
        {
            "id": 4,
            "number": "100.280",
            "name": "admin.development",
            "level": 2
        },
        {
            "id": 5,
            "number": "260.261",
            "name": "operation.content",
            "level": 3
        },
        {
            "id": 6,
            "number": "260.262",
            "name": "operation.promote",
            "level": 3
        },
        {
            "id": 7,
            "number": "260.263",
            "name": "operation.service",
            "level": 3
        }
    ];

    var res = [];
    var dicKey = {};
    data.forEach((m,i) => {
        var mStr = String(m.name).substr(0,String(m.name).indexOf(".")+1);
        var isParent = String(m.name).charAt(m.name.length -1) == ".";
        if(dicKey[mStr] == undefined)
        {
            dicKey[mStr] = [];
            if(!isParent)   dicKey[mStr].push(i);
        }else{
            dicKey[mStr].push(i);
        }
    });
    function getTreeJson(obj){
        var key = String(obj.name).charAt(obj.name.length -1) == "." ? obj.name:String(obj.name).substr(String(obj.name).indexOf(".")+1) + ".";
        if(dicKey[key])
        {
            dicKey[key].forEach(c =>{
                if(!obj.children) obj.children = [];
                obj.children.push(data[c]);
                getTreeJson(data[c]);
            });
        }
    }
    data.forEach(m => {
        if(String(m.name).charAt(m.name.length -1) == ".")
        {
            res.push(m);
            getTreeJson(m);
        }
    });

    // 打印
    console.log(JSON.stringify(dicKey));
    console.log(JSON.stringify(res));

为情所困
var data = [
    {
        "id":1,
        "number":"100.000",
        "name": "admin.",
        "level":1,
        "children": [
            {
                "id": 2,
                "number": "100.210",
                "name": "admin.marketing",
                "level": 2
            },
            {
                "id": 3,
                "number": "100.260",
                "name": "admin.operation",
                "level": 2,
                "children": [
                    {
                        "id": 5,
                        "number": "260.261",
                        "name": "operation.content",
                        "level": 3
                    },
                    {
                        "id": 6,
                        "number": "260.262",
                        "name": "operation.promote",
                        "level": 2
                    },
                    {
                        "id": 7,
                        "number": "260.263",
                        "name": "operation.service",
                        "level": 2
                    }
                ]
            },
            {
                "id": 4,
                "number": "100.280",
                "name": "admin.development",
                "level": 2
            }
        ]
    }
]
var arr = [];
function selirizeData(data){
    data.forEach(function(element) {
        var item = {};
        for(val in element){
            if(val !== 'children'){
                item[val] = element[val];
            }else{
                selirizeData(element['children']);
            }
        }
        arr.push(item);
    }, this);
};
selirizeData(data);
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!