-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

7
  • Possible duplicate of Deep comparison of objects/arrays Commented Aug 8, 2017 at 11:21
  • 1
    both are diffrent @Rajesh look first and than comment Commented Aug 8, 2017 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 Commented Aug 8, 2017 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. Commented Aug 8, 2017 at 11:26
  • 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. Commented Aug 8, 2017 at 11:30

6 Answers 6

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);

Sign up to request clarification or add additional context in comments.

2 Comments

If you expect only 1 value, use array.find instead of array.filter
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!
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; }

2 Comments

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.
Could you explain your code please, I have never used hash tables? Thanks
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);

Comments

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);

Comments

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));

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.