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的方法,自己憋了一下午實在寫不出來,謝謝了! !
習慣沉默2017-05-19 10:46:02
時間倉促,還沒寫註釋,你運行一下看看~
為了方便我也沒有用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>
天蓬老师2017-05-19 10:46:02
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)) );