将具有父Owner属性的数组映射到带子子属性的Array

时间:2018-05-28 08:43:31

标签: javascript arrays

您好我正在尝试映射如下数组:

var currentModel = [{id: 'Iron Man (CPC Avengers)', label: 'Iron Man', unit: 'CPC Avengers', level: '2', parent: '', children: []}, {id: 'Thor (Avengers Core Asgard)', label: 'Thor', unit: 'Avengers Core Asgard', level: '3', parent: 'Iron Man', children: []}, {id: 'Loki (Avengers Asgard)', label: 'Loki', unit: 'Avengers Asgard', level: '4', parent: 'Thor', children: []}, {id: 'Hela (Avengers Asgard)', label: 'Hela', unit: 'Avengers Asgard', level: '4', parent: 'Thor', children: []}, {id: 'Groot (Guardians of Galaxy)', label: 'Groot', unit: 'Guardians of Galaxy', 'level: '5', parent: 'Loki', children: []}, {id: 'Captain America (Avengers Core Earth)', label: 'Captain America', unit: 'Avengers Core Earth', level: '3', parent: 'Iron Man', children: []}, {id: 'Black Widow (Avengers Earth)', label: 'Black Widow', unit: 'Avengers Earth', level: '4', parent: 'Captain America', children: []}, {id: 'Hulk (Avengers Earth)', label: 'Hulk', unit: 'Avengers Earth', level: '4', parent: 'Captain America', children: []}]

进入下面的数组

var requiredModel = [ {id: 'Iron Man (CPC Avengers)', label: 'Iron Man', unit: 'CPC Avengers', level: '2', parent: '', children: [ { id: 'Thor (Avengers Core Asgard)', label: 'Thor', unit: 'Avengers Core Asgard', level: '3', parent: 'Iron Man', children: [ { id: 'Loki (Avengers Asgard)', label: 'Loki', unit: 'Avengers Asgard', level: '4', parent: 'Thor', children: [ { id: 'Groot (Guardians of Galaxy)', label: 'Groot', unit: 'Guardians of Galaxy', level: '5', parent: 'Loki', children: []} ]}, {id: 'Hela (Avengers Asgard)', label: 'Hela', unit: 'Avengers Asgard', level: '4', parent: 'Thor', children: []}]},{id: 'Captain America (Avengers Core Earth)', label: 'Captain America', unit: 'Avengers Core Earth', level: '3', parent: 'Iron Man', children: [{ id: 'Black Widow (Avengers Earth)', label: 'Black Widow', unit: 'Avengers Earth', level: '4', parent: 'Captain America', children:[]}, { id: 'Hulk (Avengers Earth)', label: 'Hulk', unit: 'Avengers Earth', level: '4', children: []}]}]}]

我尝试了关于currentModel的事情:

步骤1:具有各自级别的单独数组

var currentModelLvl2= currentModel.filter(function(item){ return item.level === '2'}); var currentModelLvl3= currentModel.filter(function(item){ return item.level === '3'}); var currentModelLvl4= currentModel.filter(function(item){ return item.level === '4'}); var currentModelLvl5= currentModel.filter(function(item){ return item.level === '5'});

步骤2:循环currentModelLvl4和currentModelLvl5并将currentModelLvl4的items数组添加到currentModelLvl5的children数组中,其中currentModelLvl4.label === currentModelLvl5.parent

for (var i = 0; i < currentModelLvl4.length; i++) {
var currentModelItem4 = currentModelLvl4[i];
for (var j = 0; j < currentModelLvl5.length; j++) {
    var currentModelItem5 = currentModelLvl4[j];
    if (currentModelItem4.label === currentModelItem5.parent) {
        currentModelItem4.children.push( currentModelItem5);
    }
}}

但是我的循环卡住并使我的浏览器崩溃是我的方法有问题,或者有更好的方法来做到这一点。

2 个答案:

答案 0 :(得分:1)

var currentModel = [{id: 'Iron Man (CPC Avengers)', label: 'Iron Man', unit: 'CPC Avengers', level: '2', parent: '', children: []}, {id: 'Thor (Avengers Core Asgard)', label: 'Thor', unit: 'Avengers Core Asgard', level: '3', parent: 'Iron Man', children: []}, {id: 'Loki (Avengers Asgard)', label: 'Loki', unit: 'Avengers Asgard', level: '4', parent: 'Thor', children: []}, {id: 'Hela (Avengers Asgard)', label: 'Hela', unit: 'Avengers Asgard', level: '4', parent: 'Thor', children: []}, {id: 'Groot (Guardians of Galaxy)', label: 'Groot', unit: 'Guardians of Galaxy', level: '5', parent: 'Loki', children: []}, {id: 'Captain America (Avengers Core Earth)', label: 'Captain America', unit: 'Avengers Core Earth', level: '3', parent: 'Iron Man', children: []}, {id: 'Black Widow (Avengers Earth)', label: 'Black Widow', unit: 'Avengers Earth', level: '4', parent: 'Captain America', children: []}, {id: 'Hulk (Avengers Earth)', label: 'Hulk', unit: 'Avengers Earth', level: '4', parent: 'Captain America', children: []}];

var children = {};
currentModel.forEach(model => {
  children[model.parent] = children[model.parent] || [];
  children[model.parent].push(model);
  children[model.label] = model.children = children[model.label] || model.children;
});

console.log(children[""]);

答案 1 :(得分:0)

this should do it
var values = [{
    "id": "uniqueParentId1",
    "children": [{
      "childProp1": "test1",
      "childProp2": "test3"
    }]
  },
  {
    "id": "uniqueParentId2",
    "children": [{
        "childProp1": "somevals",
        "childProp2": "other vals"
      },
      {
        "childProp1": "somevals 1",
        "childProp2": "other vals 1"
      }
    ]
  }
];

var result = values.map(value =>
  value.children.map(child => Object.assign({ parentId: value.id }, child))
).reduce((l, n) => l.concat(n), []);

console.log(result);