MZ [Javascript] Most efficient way to compare arrays of objects?

What is the most efficient way to compare arrays of objects and return only the matches?

How to find and return a new array of only the matches that has matches of color & id;
The rest of the properties can be anything else.

Example:
Code:
const a = [{color:'blue',id:5}, {color:'red',id:7, type:4}, {color:'green',id:7}, {color:'blue',id:20}];
const b = [{color:'red',id:12}, {color:'green', id:7, tag:'extraData'}, {color:'red', id:7, type:4}];

Then the newly created array would be...
Code:
[{color:'green',id:7}, {color:'red',id:7}]

Is the best way really just to iterate through both arrays and check the specified properties and if they match push the the match into a new array? That's the way I'm about to go for now I think...

Thanks for reading. :popcorn:
The easiest (but least efficient) way is to loop through array b for each element of array a:
Code:
const result = [];
for (const element of a) {
    const found = b.find(e => e.color === element.color && e.id === element.id);
    if (found) {
        result.push(found);
    }
}
Do not be misled by Array.prototype.find(). This is still a for-loop within a for-loop, which is inefficient because it has complexity O(n^2).

The most efficient way is to use a so-called "hash map" (aka "hash set") or a "hash table" (aka "dictionary"). This allows you to loop through each array only once. Meaning you now have a complexity of O(n), which is as good as it gets. In JavaScript, all objects are dictionaries.
Code:
function getKey(e) {
    return JSON.stringify({color: e.color, id: e.id});
}

const dictionary = {};
for (const element of a) {
    const key = getKey(element);
    dictionary[key] = true;
}

const result = b.filter(e => dictionary[getKey(e)]);
This will give you all the elements in b, for which there is an element in a, that has the same color and id.

You'll obviously have to modify this code depending on your precise need.
My code was based on the fact that you only seemed to care about color and id.

By the way, the result will be:
Code:
[{color:'green', id:7, tag:'extraData'}, {color:'red', id:7, type:4}]
rather than:
Code:
[{color:'green',id:7}, {color:'red',id:7}]

If you really want to get rid of the tag and type, your code will need to look more like this:
Code:
const result = b.filter(e => dictionary[getKey(e)]).map(e => { return { color: e.color, id: e.id }; });
I would do something like this:

Code:
const matchingItems = a.filter(item => b.find(({color, id}) => color === item.color && id === item.id));
If your arrays are small, honestly, i wouldn't care much about asymptotics and go with whatever is more readable (Hudell's solution, for example). This would actually probably be faster too because array traversal is fast.

I'm not very fond of the idea of using a hash map either, because altough the query is O(1) the actual time it takes is usually higher than other methods such as arrays or even binary search trees, specially when the things being hashed are complex like objects with strings and stuff. So i'd use it only if the lists are really big and the constant factor doesn't matter much.

If your arrays are medium-sized (about 200-1000 elements), probably keeping them sorted and doing binary search is best: asymptotically it's O(n log n) and it's probably faster than hashing in practice. The "keeping them sorted" part can also be done with binary search, but insertions could become as bad as O(n) because of element relocation. Still better than sorting the list everytime, which would be O(n log n) for a standard comparison sort, but far worse than the conventional O(1) for unsorted arrays and hash tables. If you're going for something more insert-intensive, maybe a balanced search tree would be better.

I'm not going to try and implement binary search here (and probably embarrass myself with an off-by-one error xd) but there's plenty of references on the internet already. Here's some:


Here's one about trees: https://www.geeksforgeeks.org/implementation-binary-search-tree-javascript/

And some implementations, if you don't want to bother writing them:
@Nolonar @Hudell @ Masked
Thanks best friends! Thanks for the knowledge drop 😉
Got the info I needed. Think for now at least I will probably go with a variation of what Hudell has shown.

Thanks again! ☺️👍

Original thread: https://forums.rpgmakerweb.com/threads/javascript-most-efficient-way-to-compare-arrays-of-objects.127514/