javascript - Looking for js data processing methods
怪我咯
怪我咯 2017-05-19 10:44:02
0
2
590
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"
                }]
            }]
        }]

I’m looking for a way to convert data into result. I’ve been stuck on it all afternoon and I really can’t write it out. Thank you! !

怪我咯
怪我咯

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

reply all(2)
習慣沉默

I’m in a hurry and haven’t written any comments yet. You can run it and see~
For convenience, I didn’t use ES6 to write it, so I’ll make do with it~

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

If you can ensure that the order of data is a, a.b, a.b.c, you can also reduce more than a dozen lines of code:

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)) );
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!