如何在不使用Underscore或Lodash的情况下在javascript中组合两个对象数组?

时间:2017-08-08 11:19:11

标签: javascript node.js

我有两个阵列

var a=[
  {_id:1,name:'a'},
  {_id:2,name:'b'},
  {_id:3,name:'c'},
]

var b=[
  {key:1,dis:0.5},
  {key:2,dis:0.9},
  {key:3,dis:10}
]

这两个都有_idkey,我想要的是_idkey是否相同,我想要一个看起来像这样的数组

var result=[
  {_id:1,name:'a',dis:0.5},
  {_id:2,name:'b',dis:0.9},
  {_id:3,name:'c',dis:1},   
]

注意:我不想使用__.map

等任何_.extend函数模拟器

由于

6 个答案:

答案 0 :(得分:4)

以下是array#findarray#map

的解决方案



var a=[
  {_id:1,name:'a'},
  {_id:2,name:'b'},
  {_id:3,name:'c'},
]

var b=[
  {key:1,dis:0.5},
  {key:2,dis:0.9},
  {key:3,dis:10}
]

var merged = a.map(o => {
  let temp = b.find(obj => obj.key === o._id);
  let res = Object.assign(o,temp);
  delete res.key;
  return res;
});

console.log(merged);




答案 1 :(得分:3)

您可以将哈希表用于相同的标识符。哈希表包含结果集的新生成对象。

首先,创建一个新对象并将其映射并存储在哈希表中。然后在另一个数组上的第二个循环中,将缺少的属性添加到对象中。

结果是一个包含新对象的数组。

var a = [{ _id: 1, name: 'a' }, { _id: 2, name: 'b' }, { _id: 3, name: 'c' }],
    b = [{ key: 1, dis: 0.5 }, { key: 2, dis: 0.9 }, { key: 3, dis: 10 }, { key: 4, dis: 42 }],
    hash = Object.create(null),
    result = a.map(function (o) {
        return hash[o._id] = { _id: o._id, name: o.name };
    });
    
b.forEach(function (o) {
    if (!hash[o.key]) {
        result.push(hash[o.key] = { _id: o.key, name: 'unknown' });
    }
    hash[o.key].dis = o.dis;
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

简单使用如下

var result = [];
for(var i = 0; i <= a.length-1; i++){
     for(var j = 0; i <= b.length-1; j++){
         if(a[i]._id == b[j].key){
          result.push(id: a[i]._id, name: a[i].name, dis: b[j].dis)
         }
     }
}
console.log(result);

答案 3 :(得分:1)

试试这个:

    if (a.length&& b.length) {
        for (var i = 0; i < a.length; i++) {
            for (var j = 0; j < b.length; j++) {
                if (a[i]._id == b[j].key) {
                    a[i].dis = b[j].dis;
                }
            }
        }
    }

    console.log(a);

OR

    var result = [];

    if (a.length&& b.length) {
        for (var i = 0; i < a.length; i++) {
            var obj = {
                _id: a[i]._id,
                name: a[i].name,
                dis: ""
            }
            for (var j = 0; j < b.length; j++) {
                if (a[i]._id == b[j].key) {
                    obj.dis = b[j].dis;
                }
            }
            result.push(obj);
        }
    }

    console.log(result);

答案 4 :(得分:1)

AFAIK这是O(N)而不是O(N ^ 2),就像大多数其他答案一样......

var a = [{
    _id: 1,
    name: 'a'
}, {
    _id: 2,
    name: 'b'
}, {
    _id: 3,
    name: 'c'
}]

var b = [{
    key: 1,
    dis: 0.5
}, {
    key: 2,
    dis: 0.9
}, {
    key: 3,
    dis: 10
}]

function merge(a, b) {
    const bKeys = b.reduce((p, c) => (p.set(c.key, c), p), new Map);
    return a.map(o=>Object.assign({}, o, bKeys.get(o._id) ));

}

console.log(merge(a, b));

答案 5 :(得分:0)

var res = a.map(function(e, i){
    e.dis = b.find(function(eb){return eb.key == e._id;}).dis;
    return e;
});

// res:[{&#34; _id&#34;:1,&#34; name&#34;:&#34; a&#34;,&#34; dis&#34;:0.5}, {&#34; _id&#34;:2&#34;名称&#34;:&#34; b&#34;&#34; DIS&#34;:0.9},{&#34; _id&#34 ; 3&#34;名称&#34;:&#34; C&#34;&#34; DIS&#34; 10}]