在javascript中基于id排列两个不等长的数组

时间:2014-12-07 14:11:20

标签: javascript jquery arrays underscore.js

数组1:

[{Id:1,Name:'abc'},{Id:5,Name:'bcd'},{Id:3,Name:'xyz'}]

数组2:

[{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:1,Name:'Ajay1'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4']

在重新排列两个数组时,输出应该是:

数组1:

[{Id:1,Name:'abc'},{Id:3,Name:'xyz'},{Id:5,Name:'bcd'}]

数组2:

[{Id:1,Name:'Ajay1'},{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}] 

OR

[{Id:1,Name:'Ajay1'},{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:4,Name:'Ajay4'},{Id:2,Name:'Ajay2'}] 

让我知道上面的最佳方式。两个数组中的现有ID应该是相同的顺序,其中一个数组中的额外ID应该在最后。

2 个答案:

答案 0 :(得分:0)

我希望这就是你要找的东西

var Arr1 = [{Id:1,Name:'Ajay1'},{Id:5,Name:'Ajay5'},{Id:3,Name:'Ajay3'}];
var Arr2 = [{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:1,Name:'Ajay1'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}];

var finalArr = [];
var unMatchedArr = [];
Arr2.forEach(function(item) {
  var inArr = Arr1.filter(function(a1) {
    return a1.Id === item.Id;
  });

  if(inArr.length) {
    finalArr.push(item);
  }

  else {
    unMatchedArr.push(item);
  }

});

var finalArr = finalArr.concat(unMatchedArr);
snippet.log(JSON.stringify(finalArr));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

finalArr包含结果

答案 1 :(得分:0)

您可以使用下划线sortBy对第一个数组进行排序。

然后合并filterevery以找到剩余的。最后,您可以使用本机concat方法组合两个数组来创建第二个数组,如下所示:

&#13;
&#13;
var arr1 =[{Id:1,Name:'abc'},{Id:5,Name:'bcd'},{Id:3,Name:'xyz'}],
arr2 = [{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:1,Name:'Ajay1'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}],
  newArr1 = _.sortBy(arr1, "Id"),
  diff = _.filter(arr2, function(obj) {
    return _.every(arr1, function(elm) {
      return elm.Id != obj.Id;
    });
  }),
  newArr2 = newArr1.concat(diff);

console.log(newArr1,newArr2);
snippet.log(JSON.stringify(newArr1));
snippet.log(JSON.stringify(newArr2));
&#13;
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
&#13;
&#13;
&#13;

您也可以使用其他方式同时组合rejectsome

&#13;
&#13;
var arr1 = [{Id:1,Name:'Ajay1'},{Id:5,Name:'Ajay5'},{Id:3,Name:'Ajay3'}],
  arr2 = [{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:1,Name:'Ajay1'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}],
  newArr1 = _.sortBy(arr1, "Id"),
  diff = _.reject(arr2, function(obj) {
    return _.some(arr1, function(elm) {
      return elm.Id == obj.Id;
    });
  }),
  newArr2 = newArr1.concat(diff);

console.log(newArr1, newArr2);
snippet.log(JSON.stringify(newArr1));
snippet.log(JSON.stringify(newArr2));
&#13;
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
&#13;
&#13;
&#13;

<强>更新

&#13;
&#13;
var arr1 =[{Id:1,Name:'abc'},{Id:5,Name:'bcd'},{Id:3,Name:'xyz'}],
arr2 = [{Id:3,Name:'Ajay3'},{Id:5,Name:'Ajay5'},{Id:1,Name:'Ajay1'},{Id:2,Name:'Ajay2'},{Id:4,Name:'Ajay4'}],
  newArr1 = _.sortBy(arr1, "Id"),
  matches = _.filter(_.sortBy(arr2, "Id"), function(obj) {
    return _.find(arr1, function(elm) {
      return elm.Id == obj.Id;
    });
  }),
  diff = _.difference(arr2, matches),
  newArr2 = matches.concat(diff);
snippet.log(JSON.stringify(newArr1));
snippet.log(JSON.stringify(newArr2));
&#13;
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
&#13;
&#13;
&#13;