javascript - 求js处理数据方法
怪我咯
怪我咯 2017-05-19 10:44:02
0
2
593
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"
                }]
            }]
        }]

求一个把data转换为result的方法,自己憋了一下午实在写不出来,谢谢了!!

怪我咯
怪我咯

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

全部回复(2)
習慣沉默

时间仓促,还没有写注释,你运行一下看看~
为了方便我也没有用ES6来写,凑合着看吧~

<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)) );

如果能保证 数据的顺序为 a,a.b,a.b.c,还可以减少十几行代码:

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)) );
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!