'Efficient way to merge dictionaries while removing duplicates using javascript?

Given an array of dictionaries in javascript, I want to merge them so that they don't contain any duplicates. For example given

jsons1 = [{"a": "xyz"}, {"a": 12}]
jsons2 = [{"a": "xyz"}, {"a", 13}]

I want to return a new array that removes the duplicates while merging

jsons3 = [{"a": "xyz"}, {"a": 12}, {"a": 13}]

order isnt important

This is what I did:

let jsons1 = [
    {
        "a": "xyz"
    
    }, 
    {
        "a": 4
    
    }, 
    {
        "a": 1
    
    }, 
  
]


let jsons2 = [
    {
        "a": "xyz"
    
    }, 
    {
        "a": 4
    
    }, 
    {
        "a": 6
    
    }, 
  
]

const val = [jsons1, jsons2]

let filtered = []
for(json of val) {
    for(obj of json) {
    let match = false;
    for(dict of filtered) {
        if(JSON.stringify(dict) === JSON.stringify(obj)) {
        match = true;
        break
      }
    }
    if(match == true) {
        continue
    }
    filtered.push(obj)

    }
}

console.log(filtered)

https://jsfiddle.net/awb6qnzo/6/

Basically I create a new array called filtered and I then iterate through each jsons array. I then iterate through filtered to check if that obj is already in it.

although it works, its extremely inefficient. Is there a simpler way?

Edit: Changing question to specific values since the former is apparently not possible

Given an array of dictionaries in javascript, I want to merge them so that they don't contain any duplicates (on the key "id"). E.g.

jsons1 = [{"id": 1}, {"id": 12}]
jsons2 = [{"id": 1}, {"id": 3}]

I want to return a new array that removes the duplicates while merging

jsons3 = [{"id": 1}, {"id": 3}, {"id": 12}]


Solution 1:[1]

Let's reword the problem and pretend we have a function equals that checks if two objects are the same, first.

We want to concatenate all the values in jsons2 that satisfy some condition.

To do that, we need to filter out objects that don't satisfy the condition.

This condition is that every object in jsons1 does not equal the object in jsons2.

Another way to put this is the negation of some object in jsons1 is equal to the object in jsons2.

jsons1.concat(
    jsons2.filter(
        (a) => !jsons1.some((b) => equals(a, b))
    )
);

You can write equals anyway you want, as long as it functions correctly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hittingonme