如何将2个数组与对象合并为一个?

时间:2016-06-01 07:41:53

标签: javascript arrays

我有2个阵列:

  1. [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}]

  2. [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}]

  3. 如何将这些数组合并到一个数组中与唯一对象(用于唯一检查'名称')?

6 个答案:

答案 0 :(得分:4)

filterfind的组合将起作用

var a = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}];

var b = [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}];

var c = a.filter(a => b.find(b => b.name === a.name) === undefined).concat(b);

console.table(c);

enter image description here

您还可以制作通用uniqueByKey功能

var uniqueByKey = (key, xs)=>
  xs.reduce((ys,x)=>
    ys.find(y=> y[key] === x[key]) === undefined
      ? ys.concat([x])
      : ys,
    []);

var a = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}];

var b = [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}];

var c = uniqueByKey('name', a.concat(b));

console.table(c);

enter image description here

如果您要处理特别大的数据集,使用Set缓存而不是Array.prototype.find可能会更好。

var uniqueByKey = (key, xs)=>
  xs.reduce(([set, ys], x)=>
    set.has(x[key])
      ? [set, ys]
      : [set.add(x[key]), ys.concat([x])]
    , [new Set, []]
  ) [1];

var a = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}];

var b = [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}];

var c = uniqueByKey('name', a.concat(b));

console.table(c);

enter image description here

答案 1 :(得分:1)

var arr1 = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}]
var arr2 = [{name:'test', lastname: 'test', gender:'f'},{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}]

var newArray =arr1.concat(arr2);
var unique = {};
var result = [];


newArray.forEach(function (elem) {
  if (!unique[elem.name]) {
    result.push(elem);
    unique[elem.name] = true;
  }
});
console.log(result);
document.write(JSON.stringify(result));

答案 2 :(得分:0)

你可以做些什么来检查重复的元素,最快的方法是将所有元素添加到你想要作为键的名称的对象,因为访问键非常快:

var unified = {},
    unifiedArray = [],
    first = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}],
    second = [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}],
    addToUnified = function(obj) {
        if (unified[obj.name]) {
            alert('duplicate detected!\n' + JSON.stringify(obj) + '\n' + ' has same name as\n'  + JSON.stringify(unified[obj.name]));
        } else {
            unifiedArray.push(obj);
            unified[obj.name] = obj;
        }
    }
first.forEach(addToUnified);
second.forEach(addToUnified);

如果您只想获得报告,则不需要unifiedArray。如果您不需要该报告,请删除alert

答案 3 :(得分:0)

对于新数组,您可以使用对象作为哈希表进行查找。



nvarchar(5)




ES6



var array1 = [{ name: 'test', lastname: 'test', gender: 'f' }, { name: 'test1', lastname: 'test1', gender: 'f' }, { name: 'test2', lastname: 'test2', gender: 'm' }],
    array2 = [{ name: 'test21', lastname: 'test21', gender: 'f' }, { name: 'test1', lastname: 'test1', gender: 'f' }, { name: 'test2', lastname: 'test2', gender: 'm' }, { name: 'test22', lastname: 'test22', gender: 'm' }],
    result = function (a1, a2) {
        function merge(a) {
            this[a.name] = this[a.name] || r.push(a);
        }

        var t = Object.create(null),
            r = [];
        a1.forEach(merge, t);
        a2.forEach(merge, t);
        return r;
    }(array1, array2);

console.log(result);




答案 4 :(得分:0)

如果您拥有像Object.prototype.compare()这样的Object方法,那就很容易了。让我们来吧。

Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};

Object.prototype.compare()比较两个对象,以便完美匹配所有属性及其值。因此,与接受的答案不同,我们不是比较单个属性而是所有属性。现在它是一个简单的减少任务。我们来看看



Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};

var arr1 = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}],
    arr2 = [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}],
  united = arr1.reduce((p,c) => p.find(f => f.compare(c)) ? p : p.concat(c),arr2);
console.log(JSON.stringify(united));




答案 5 :(得分:0)

var a = [{name:'test', lastname: 'test', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'}];

var b = [{name:'test21', lastname: 'test21', gender:'f'},{name:'test1', lastname: 'test1', gender:'f'},{name:'test2', lastname: 'test2', gender:'m'},{name:'test22', lastname: 'test22', gender:'m'}];

var c = a.filter(a => b.find(b => b.name === a.name) === undefined).concat(b);

console.table(c);