基于id合并两个数组

时间:2015-11-09 11:09:37

标签: javascript arrays merge

我有2个对象数组:

var parents = [
  { id: 77777, data: {}},
  { id: 88888, data: {}},
  { id: 99999, data: {}}
]


var children = [
  {
    belongTo: 77777,
    data: [
      { id: 111, data: {}},
      { id: 222, data: {}}
    ]},
  {
    belongTo: 99999,
    data: [
      { id: 333, data: {}}
    ]
  }
]

我想将父母和孩子合并到:

var all = [
  { id: 77777, data: {}},
  { id: 111, data: {}},
  { id: 222, data: {}},
  { id: 88888, data: {}},
  { id: 99999, data: {}},
  { id: 333, data: {}}
]

我一直在尝试使用拆分或将它们合并在一起然后变平,但它总是非常复杂。

实现合并的最简单方法是什么?

我的复杂方式(不起作用)

children.map(function(child) {
  var parentIndex = parents.map(function(x) {return x.id}).indexOf(child.id)

  parents.splice(parentIndex + 1, 0, child.data)
})

[].concat.apply([], parents)

3 个答案:

答案 0 :(得分:3)

你只需要一些嵌套循环来获取它: http://jsfiddle.net/hpfgqc4m/2/

var parents = [
  { 
    id: 77777,
    data: {}
  },
  { 
    id: 88888,
    data: {}
  },
  { 
    id: 99999,
    data: {}
  }
]


var children = [
  {
    belongTo: 77777,
    data: [{
      id: 111,
      data: {}
    },
    {
      id: 222,
      data: {}
    }]
  },
  {
    belongTo: 99999,
    data: [{
      id: 333,
      data: {}
    }]
  }
]
var all = [];
parents.forEach(function(e, k) {
    all.push(e);
    children.forEach(function(el, key) {
        if (el.belongTo == e.id) {
            el.data.forEach(function(element) {
                all.push(element);            
            });                
        }
    });
});
console.log(all);  

答案 1 :(得分:1)

试试这个



var parents = [{
    id: 77777,
    data: {}
}, {
    id: 88888,
    data: {}
}, {
    id: 99999,
    data: {}
}];

var children = [{
    belongTo: 77777,
    data: [{
        id: 111,
        data: {}
    }, {
        id: 222,
        data: {}
    }]
}, {
    belongTo: 99999,
    data: [{
        id: 333,
        data: {}
    }]
}];

function merge(parents, children) {
  var parentsLen = parents.length,
      childLen = children.length,
      i, j,
      result = [];
  
  for (i = 0; i < parentsLen; i++) { 
    result.push(parents[i]);
    
    for (j = 0; j < childLen; j++) {
      if (parents[i].id === children[j].belongTo) {
        result = result.concat(children[j].data);
      }
    };
  }
  
  return result;
}

console.log(merge(parents, children));
&#13;
&#13;
&#13;

修复您的版本

&#13;
&#13;
var parents = [{
    id: 77777,
    data: {}
}, {
    id: 88888,
    data: {}
}, {
    id: 99999,
    data: {}
}];

var children = [{
    belongTo: 77777,
    data: [{
        id: 111,
        data: {}
    }, {
        id: 222,
        data: {}
    }]
}, {
    belongTo: 99999,
    data: [{
        id: 333,
        data: {}
    }]
}];

children.forEach(function(child) {
  var parentIndex = parents.map(function(x) {return x.id}).indexOf(child.id);
  parents.splice(parentIndex + 1, 0, child.data)
})

parents = [].concat.apply([], parents)

console.log(parents);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用Higher Order Functions和一点ES6

public function postRegister(Request $request)
{
    $this->validate($request->all(), [
        'company_name' => 'unique:companies',
        // And the other rules, like email unqiue, etc..
    ]);  

    Auth::login($this->create($request->all()));

    return redirect($this->redirectPath());
}

可以进一步重构吗?