1

Mock

a = [{id:123},{id:1234},{id:12345}]
b = [{id:123},{id:1234},{id:123456}]

Code

a.filter((element)=> {
  return b.some((ele) =>{
    if (element.id === ele.id) {
     return matched[element.id] = element.id
    } else {
      return unmatched[element.id] = element.id
    }
 });
});

Expected output

matched = {123: 123, 1234: 1234}
unmatched = {12345: 12345}

output

unmatched = {123: 123, 1234: 1234, 12345: 12345}
matched = {123: 123, 1234: 1234}

could any one help me out here. I am trying to compare two arrays and get the difference into different objects

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
  • Returning the assignment into a `.some()` is an odd pattern, I would expect the `.some` to always be `true` unless the ID is `0` or another falsey value. – DBS Apr 07 '21 at 12:37
  • Why is the key and the value the same in the output? – Heretic Monkey Apr 07 '21 at 12:40
  • The answers to [How to get the difference between two arrays of objects in JavaScript](https://stackoverflow.com/q/21987909/215552) will get you the actual unmatched objects. – Heretic Monkey Apr 07 '21 at 12:41

2 Answers2

1

You could take a Set or object for a and iterate b for getting the poperty into the right object.

const
    a = [{ id: 123 }, { id: 1234 }, { id: 12345 }],
    b = [{ id: 123 }, { id: 1234 }, { id: 123456 }],
    aSet = new Set(a.map(({ id }) => id)),
    [matched, unmatched] = b.reduce((r, { id }) => {
        Object.assign(r[1 - aSet.has(id)], { [id]: id });
        return r;
    }, [{}, {}]);
    

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

An approach by using the objects directly

const
    a = [{ _id: '123', index: 3 }, { _id: '1234', index: 3 }],
    b = [{ _id: '123', index: 2 }, { _id: '12345', index: 3 }],
    aSet = new Set(a.map(({ _id }) => _id)),
    [matched, unmatched] = b.reduce((r, o) => {
        r[1 - aSet.has(o._id)].push(o);
        return r;
    }, [[], []]);
    
console.log(matched);
console.log(unmatched);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • can I know what is 'aSet' here –  Apr 07 '21 at 12:51
  • it is an instance of [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) with the ids from `a`. – Nina Scholz Apr 07 '21 at 12:54
  • can I find the index for the matched records in array 'a'. could you help me with that –  Apr 07 '21 at 13:51
  • what index do you what and where should it it apply to? – Nina Scholz Apr 07 '21 at 13:58
  • I have original data record which will consists of matched and other set of objects and I need to update the matched and unmatched object records based on the index position. so for that I need to find the indexes of matched records against the original array of objects. For unmatched I dont require the indexes. lets consider "a" as the original record and "b" is the update I am receiving –  Apr 07 '21 at 16:34
  • you could store the index as value instead of `id` in the set (or both, it it helps ...) – Nina Scholz Apr 07 '21 at 16:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230868/discussion-between-vinay-kumar-and-nina-scholz). –  Apr 07 '21 at 19:51
0

This would work:

a = [{id:123},{id:1234},{id:12345}];
b = [{id:123},{id:1234},{id:123456}];

function compare(a, b) {
    const returnObj = { matched: [], unmatched: [] };
    a.forEach(aItem => {
        const found = b.find(bItem => JSON.stringify(aItem) === JSON.stringify(bItem));
        returnObj[found ? 'matched' : 'unmatched'].push(aItem);
    });
    return returnObj;
}

const { matched, unmatched } = compare(a, b);

console.log(matched);
console.log(unmatched);
Brother Woodrow
  • 6,092
  • 3
  • 18
  • 20