javascript - Mencari kaedah pemprosesan data js
怪我咯
怪我咯 2017-05-19 10:44:02
0
2
603
var data={
            "soft":"1111",
            "soft.type":"1111",
            "soft.condition":"1111",
            "wonder":"1111",
            "wonder.WONDER":"1111",
            "wonder.WONDER.BS_TEST":"1111",
            "wonder.WONDER.DB2数据类型":"1111",
            "wonder.WONDER.DEP_TABLE":"1111",
            "wonder.WONDER.DIC_TABLE":"1111",
            "wonder.WONDER.TABLE8_NF":"1111",
            "wonder.WONDER.基本信息":"1111",
            "wonder.WONDER.姓名":"1111",
            "wonder.WONDER.数据类型":"1111"
}
var result=[{
            "name":"soft",
            "val":"1111",
            "children":[{
                "name":"type",
                "val":"1111",
            },{
                "name":"condition",
                "val":"1111",
            }]
        },{
            "name":"wonder",
            "val":"1111",
            "children":[{
                "name":"WONDER",
                "val":"1111",
                "children":[{
                    "name":"BS_TEST",
                    "val":"1111"
                },{
                    "name":"DB2数据类型",
                    "val":"1111"
                },{
                    "name":"DEP_TABLE",
                    "val":"1111"
                },{
                    "name":"姓名",
                    "val":"1111"
                },{
                    "name":"数据类型",
                    "val":"1111"
                }]
            }]
        }]

Saya sedang mencari cara untuk menukar data kepada hasil yang saya habiskan sepanjang petang dan tidak dapat menuliskannya. !

怪我咯
怪我咯

走同样的路,发现不同的人生

membalas semua(2)
習慣沉默

Saya tergesa-gesa dan belum menulis sebarang komen lagi Sila jalankan dan lihat~
Demi kemudahan, saya tidak menggunakan ES6 untuk menulisnya, jadi saya akan buat. dengannya~

<script>
var data = {
    "soft":"1111",
    "soft.type":"1111",
    "soft.condition":"1111",
    "wonder":"1111",
    "wonder.WONDER":"1111",
    "wonder.WONDER.BS_TEST":"1111",
    "wonder.WONDER.DB2数据类型":"1111",
    "wonder.WONDER.DEP_TABLE":"1111",
    "wonder.WONDER.DIC_TABLE":"1111",
    "wonder.WONDER.TABLE8_NF":"1111",
    "wonder.WONDER.基本信息":"1111",
    "wonder.WONDER.姓名":"1111",
    "wonder.WONDER.数据类型":"1111"
};
function rebuild(data) {
    var result = [];
    Object.keys(data).map(function(key) {
        var val = data[key];
        var nameArr = key.split(".");
        var obj = result;
        nameArr.map(function(name, index) {
            var existResult = exist(obj, name);
            if (existResult) {
                obj = existResult.children;
            } else {
                var item = {name: name, children: []};
                if (index === nameArr.length - 1) item.val = val;
                obj.push(item);
            }
        });
    });
    return result;
}
function exist(arr, name) {
    var result = arr.filter(function(item) {
        return item.name === name;
    });
    return result.length > 0 ? result[0] : false;
}
console.log(rebuild(data));
</script>
Peter_Zhu
var data = {
    "soft":"1111",
    "soft.type":"1111",
    "soft.condition":"1111",
    "wonder.WONDER":"1111",
    "wonder.WONDER.BS_TEST":"1111",
    "wonder.WONDER.DB2数据类型":"1111",
    "wonder.WONDER.DEP_TABLE":"1111",
    "wonder.WONDER.DIC_TABLE":"1111",
    "wonder.WONDER.TABLE8_NF":"1111",
    "wonder.WONDER.基本信息":"1111",
    "wonder.WONDER.姓名":"1111",
    "wonder.WONDER.数据类型":"1111",
    "wonder":"1111",
}

var toResult = function(data){
    var result = [];
    var tree = {}; //缓存结构
    var route; //路径
    var parent; //父节点
    var val; //值
    for( var item in data ){
        route = item.split('.');
        val = {
            "name" : route[route.length - 1],
            "val" : data[item]
        };
        
        parent = route.filter((v,index,arr)=>index < arr.length-1).join('.');
        if( parent != '' ){
            //考虑了子项(wonder.WONDER)可能写在项(wonder)前面,如果还没初始化,则设置为{}
            tree[parent] = tree[parent] || result[result.push({}) - 1];
            //如果父节点不存在 children 数组,则创建
            tree[parent].children = tree[parent].children || [];
        }
        if( item in tree ){
            //考虑了子项(wonder.WONDER)可能写在项(wonder)前面,已经初始化为{}
            tree[item].name = val.name;
            tree[item].val = val.val;
        } else {
            if( parent != '' ){
                //如果有根节点,添加到根节点
                tree[item] = tree[parent].children[tree[parent].children.push(val) - 1];
            } else {
                //根节点,添加到result
                tree[item] = result[result.push(val) - 1];
            }
        }
    };
    delete tree;
    return result;
};

console.log( JSON.stringify(toResult(data)) );

Jika anda boleh memastikan susunan data ialah a, a.b, a.b.c, anda juga boleh mengurangkan lebih daripada sedozen baris kod:

var data = {
    "soft":"1111",
    "soft.type":"1111",
    "soft.condition":"1111",
    "wonder":"1111",
    "wonder.WONDER":"1111",
    "wonder.WONDER.BS_TEST":"1111",
    "wonder.WONDER.DB2数据类型":"1111",
    "wonder.WONDER.DEP_TABLE":"1111",
    "wonder.WONDER.DIC_TABLE":"1111",
    "wonder.WONDER.TABLE8_NF":"1111",
    "wonder.WONDER.基本信息":"1111",
    "wonder.WONDER.姓名":"1111",
    "wonder.WONDER.数据类型":"1111",
}

var toResult = function(data){
    var result = [];
    var tree = {}; //缓存结构
    var route; //路径
    var parent; //父节点
    var val; //值
    for( var item in data ){
        route = item.split('.');
        val = {
            "name" : route[route.length - 1],
            "val" : data[item]
        };
        parent = route.filter((v,index,arr)=>index < arr.length-1).join('.');
        if( parent != '' ){
            tree[parent].children = tree[parent].children || [];
            tree[item] = tree[parent].children[tree[parent].children.push(val) - 1];
        } else {
            tree[item] = result[result.push(val) - 1];
        }
    };
    delete tree;
    return result;
};

console.log( JSON.stringify(toResult(data)) );
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan