将n长度二维数组转换为n级嵌套对象

时间:2015-12-17 19:22:42

标签: javascript

我有二维数组,如下所示

var testData = [
  ["level-1", "level-2", "level-3", 109], // goes to level-n
  ["level-1", "level-2", "level-3", 25],  // goes to level-n
  ["level-1", "level-2", "level-3", 20],  // goes to level-n
  ["level-1", "level-2", "level-3", 17],  // goes to level-n
];  

我需要将其转换为以下结构

var resultData = [{
  "name": "level-1",
  "sub-name": [{
    "name": "level-2",
    "sub-name": [{
      "name": "level-3",
      "val": 109
    }]
  }]
}, {
  "name": "level-1",
  "sub-name": [{
    "name": "level-2",
    "sub-name": [{
      "name": "level-3",
      "val": 25
    }]
  }]
}, {
  "name": "level-1",
  "sub-name": [{
    "name": "level-2",
    "sub-name": [{
      "name": "level-3",
      "val": 20
    }]
  }]
}, {
  "name": "level-1",
  "sub-name": [{
    "name": "level-2",
    "sub-name": [{
      "name": "level-3",
      "val": 17
    }]
  }]
}];

我尝试了以下但是它缺少1级并且显示级别3而不是级别2。



var testData = [
  ["level-1", "level-2", "level-3", 109], // goes to level-n
  ["level-1", "level-2", "level-3", 25],  // goes to level-n
  ["level-1", "level-2", "level-3", 20],  // goes to level-n
  ["level-1", "level-2", "level-3", 17],  // goes to level-n
];
var index = 3;
var testArray = [];


function converter(d) {

  d.forEach(function(b) {
    recursive(b, 0);
  });

}

function recursive(element, i, testObj) {
  testObj = testObj || {};
  testObj.name = element[i];
  testObj.category = element[i];
  testObj.subCategory = [{}];
  ++i;
  if (i < index) {
    recursive(element, i, testObj);
  }
  if (i == index) {
    testObj.subCategory[0].name = element[index - 1];
    testObj.subCategory[0].val = element[index];
    testObj.subCategory[0].subCategory = null;
    return testObj;
  }
	document.write('<pre>' + JSON.stringify(testObj, 0, 4) + '</pre>');
}
converter(testData);
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

我相信这就是你要找的东西:

var testData = [
  ["level-1", "level-2", "level-3", 109], // goes to level-n
  ["level-1", "level-2", "level-3", 25],  // goes to level-n
  ["level-1", "level-2", "level-3", 20],  // goes to level-n
  ["level-1", "level-2", "level-3", 17],  // goes to level-n
]; 

objectify = function(vals) {
  if (vals.length <= 2) {
    return { name: vals[0], val: vals[1] }
  } else {
    var name = vals.shift()
    return { "name": name,    
             "sub-name": [objectify(vals)] }
  }
}

result = testData.map(objectify)

console.log(JSON.stringify(result))

(参见jsfiddle:https://jsfiddle.net/74o5gkmc/

答案 1 :(得分:0)

您只需要做2次修复:

修复1:在您的代码中,您假设testObj是子类别列表的内部对象。但是,您传入整个列表,而不是对象本身。考虑递归函数参数的类型目的将帮助您避免此错误。

recursive(element, i, testObj.subCategory[0]);

修复2:要删除对象属性,您需要为其分配undefined,而不是null

testObj.subCategory[0].subCategory = undefined;

答案 2 :(得分:0)

这是问题的迭代解决方案:

&#13;
&#13;
var testData = [
  ["level-1", "level-2", "level-3", 109], // goes to level-n
  ["level-1", "level-2", "level-3", 25],  // goes to level-n
  ["level-1", "level-2", "level-3", 20],  // goes to level-n
  ["level-1", "level-2", "level-3", 17],  // goes to level-n
],
  resultData= [];

testData.forEach(function(a) {
  var obj = {};
  resultData.push(obj);

  a.forEach(function(val, idx) {
    if(idx < a.length - 2) {
      obj.name= val;
      obj['sub-name']= [{}];
      obj= obj['sub-name'][0];
    }
    else {
      obj.name= a[idx - 1];
      obj.val= val;
    }
  });
});

document.body.innerHTML= '<pre>'+JSON.stringify(resultData, null, 2)+'</pre>';
&#13;
&#13;
&#13;