2017-03-01 50 views
0

我有这个简单的JSON阵列结构的Javascript:转换JSON阵列级联的JSON

[ 
    [ "1000", "Kangar","Perlis" ], 
    [ "1532", "Kangar", "Perlis" ], 
    [ "2000", "Kuala Perlis", "Perlis" ], 
    [ "6250", "Alor Setar", "Kedah" ], 
    [ "6300", "Kuala Nerang", "Kedah" ] 
] 

现在我想结构JSON这样

{ 
    "Perlis": 
    { 
     "Kangar": [ "1000", "1532" ], 
     "Kuala Perlis": [ "2000" ]     
    }, 
    "Kedah": 
    { 
     "Alor Setar":["6250"], 
     "Kuala Nerang":["2000"] 
    } 
} 

因此,如何能利用我达到这个效果JavaScript的目的?

+0

你尝试过这么远吗? – Zim84

+0

您可以使用'JSON.stringify'的[space参数](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument) – hindmost

回答

2

尝试以下

var arr = [ 
 
    ["1000", "Kangar", "Perlis"], 
 
    ["1532", "Kangar", "Perlis"], 
 
    ["2000", "Kuala Perlis", "Perlis"], 
 
    ["6250", "Alor Setar", "Kedah"], 
 
    ["6300", "Kuala Nerang", "Kedah"] 
 
]; 
 

 
var obj = {}; 
 

 
arr.forEach(function(item) { 
 
    obj[item[2]] = obj[item[2]] || {}; 
 
    obj[item[2]][item[1]] = obj[item[2]][item[1]] || []; 
 
    obj[item[2]][item[1]].push(item[0]); 
 
}); 
 

 
console.log(obj);

+1

精简版 - ((obj [row [2]] ||(obj [row [2]] = {}))[row [1]] ||(obj [row [ 2]] [row [1]] = []))。push(row [0]); }); –

2

您可以使用reduce创建哈希对象是这样的:

function transform(arr) { 
 
    return arr.reduce(function(hash, sub) { 
 
    if(hash [sub[2]]) {      // if we hashed the first-level-categry (sub[2]) 
 
     if(hash [sub[2]] [sub[1]])    // -- if we hashed the second-level category (sub[1]) 
 
     hash [sub[2]] [sub[1]].push(sub[0]); // ---- add the item (sub[0]) to that array 
 
     else         // -- otherwise 
 
     hash [sub[2]] [sub[1]] = [sub[0]]; // ---- create second-level-category placeholder (new array) that initially contains the current item (sub[0]) 
 
    } 
 
    else {         // else 
 
     hash [sub[2]] = {};      // -- create the first-level-category placeholder 
 
     hash [sub[2]] [sub[1]] = [sub[0]];  // -- create the second-level-category placeholder (new array) that initially contains the current item (sub[0]) 
 
    } 
 
    return hash; 
 
    }, {}); 
 
} 
 

 

 
var array = [ 
 
    ["1000","Kangar","Perlis"], 
 
    ["1532","Kangar","Perlis"], 
 
    ["2000","Kuala Perlis","Perlis"], 
 
    ["6250","Alor Setar","Kedah"], 
 
    ["6300","Kuala Nerang","Kedah"] 
 
]; 
 

 
console.log(transform(array));

1

var inputArr = [ 
 
    ["1000","Kangar","Perlis"], 
 
    ["1532","Kangar","Perlis"], 
 
    ["2000","Kuala Perlis","Perlis"], 
 
    ["6250","Alor Setar","Kedah"], 
 
    ["6300","Kuala Nerang","Kedah"] 
 
]; 
 
var processFunction = function(arr){ 
 

 
var outputObj = {}; 
 
arr.forEach(function(elem){ 
 
    if(!outputObj[ elem[2] ]) 
 
    outputObj[ elem[2] ] ={}; 
 
    if(!outputObj[ elem[2] ] [ elem[1] ]) 
 
    outputObj[ elem[2] ][ elem[1] ] = []; 
 
    outputObj[ elem[2] ][ elem[1] ].push(elem[0]); 
 
    
 
}); 
 
return outputObj; 
 
}; 
 

 
alert(JSON.stringify(processFunction(inputArr)));

1

var arr = [ 
 
    ["1000", "Kangar", "Perlis"], 
 
    ["1532", "Kangar", "Perlis"], 
 
    ["2000", "Kuala Perlis", "Perlis"], 
 
    ["6250", "Alor Setar", "Kedah"], 
 
    ["6300", "Kuala Nerang", "Kedah"] 
 
] 
 

 
function convert(arr) { 
 
    return arr.reduce(function (o, e) { 
 
    o[e[2]] = o[e[2]] || {}; 
 
    o[e[2]][e[1]] = o[e[2]][e[1]] || []; 
 
    o[e[2]][e[1]].push(e[0]); 
 
    return o; 
 
    }, {}); 
 
} 
 

 
console.log(convert(arr));

这是什么o[e[2]] = o[e[2]] || {}所做的就是将o[e[2]]本身,或者如果它是一个falsy值(如undefined) - 一个新的对象。这充当了简单的初始化并防止访问不存在的值。

为了安全起见,你可以添加一个检查每个数组元素的长度:

return arr.reduce(function(o, e) { 
    if (e.length === 3) { 
    ... 
    } 

    return o; 
}, {}); 
+0

谢谢你这么好的解释 –

0

你可以用reduce()做到这一点。

var data = [ 
 
    ["1000", "Kangar", "Perlis"], 
 
    ["1532", "Kangar", "Perlis"], 
 
    ["2000", "Kuala Perlis", "Perlis"], 
 
    ["6250", "Alor Setar", "Kedah"], 
 
    ["6300", "Kuala Nerang", "Kedah"] 
 
] 
 

 
var o = {} 
 
var result = data.reduce(function(r, e) { 
 
    if (!o[e[1]]) { 
 
    o[e[1]] = {[e[1]]: []} 
 
    r[e[2]] = Object.assign((r[e[2]] || {}), o[e[1]]) 
 
    } 
 
    o[e[1]][e[1]].push(e[0]) 
 
    return r 
 
}, {}) 
 

 
console.log(result)