Javascript:将JSON数组转换为级联JSON

时间:2017-03-01 09:36:28

标签: 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的对象来实现这个结果?

5 个答案:

答案 0 :(得分: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 :(得分: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));

答案 2 :(得分: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)) );

答案 3 :(得分: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]]设置为自身,或者如果它是一个假值(如undefined) - 则设置为新对象。这可以轻松初始化,并防止访问不存在的值。

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

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

  return o;
}, {});

答案 4 :(得分: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)