-2

i have two array

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}
]

from this both there is _id and key , what i want is if both _id and key are same than i want one array which look like this

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

NOTE : i don't want to use any _ functions similer like _.map or _.extend

Thanks

asmmahmud
  • 4,844
  • 2
  • 40
  • 47
kumbhani bhavesh
  • 2,189
  • 1
  • 15
  • 26
  • Possible duplicate of [Deep comparison of objects/arrays](https://stackoverflow.com/questions/13142968/deep-comparison-of-objects-arrays) – Rajesh Aug 08 '17 at 11:21
  • 1
    both are diffrent @Rajesh look first and than comment – kumbhani bhavesh Aug 08 '17 at 11:22
  • 5
    Please show what you have tried. Stackoverflow is not a free code writing service. The objective is to help you fix **your code** – charlietfl Aug 08 '17 at 11:23
  • @kumbhanibhavesh my apologies. Your title mislead me. I take my vote back, but the right title would be to *merge objects in 2 objects with same value for different key*. Also, since your question does not show effort, we do not have a problem statement. This is a requirement. So I'd request you to share your code and where are you stuck in it. – Rajesh Aug 08 '17 at 11:26
  • @Rajesh i alredy wrote my code at there and also wrote which output i want – kumbhani bhavesh Aug 08 '17 at 11:27
  • well @nina solved it – kumbhani bhavesh Aug 08 '17 at 11:28
  • 3
    @kumbhanibhavesh You wrote input and output format. This is a **requirement**. A code is something that you have tried to achieve on your own and failed. Also please mind the tone. – Rajesh Aug 08 '17 at 11:30

6 Answers6

4

Here is a solution with array#find and array#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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • 1
    If you expect only 1 value, use `array.find` instead of `array.filter` – Rajesh Aug 08 '17 at 11:32
  • 1
    an elegant and modern solution, which was nearly exactly what I was going to write, but I did not think about using delete. that is a nice touch. **edit**: Nice @Rajesh, I will start using `array.find` more often now! – Pjetr Aug 08 '17 at 11:33
3

You could use a hash table for the same identifier. The hash table contains the new generated objects of the result set.

First, a new object is creted and mapped and also store in the hash table. Then in a second loop over the other array, then missing property is added to the object.

The result is an array with new objects.

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Should we really answer such question? This is a requirement and not a problem statement. But considering this as an opinionated action, i would hold my vote. – Rajesh Aug 08 '17 at 11:29
  • Could you explain your code please, I have never used hash tables? Thanks – Ivan Aug 08 '17 at 11:49
1

Simple use as follows

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);
Nasiruddin Saiyed
  • 1,438
  • 1
  • 13
  • 31
1

Try this:

    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);
Bimal Das
  • 1,882
  • 5
  • 28
  • 53
1

AFAIK this is O(N) instead of O(N^2) like most of the other answers...

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));
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
0
var res = a.map(function(e, i){
    e.dis = b.find(function(eb){return eb.key == e._id;}).dis;
    return e;
});

// res : [{"_id":1,"name":"a","dis":0.5},{"_id":2,"name":"b","dis":0.9},{"_id":3,"name":"c","dis":10}]

Daphoque
  • 4,421
  • 1
  • 20
  • 31