Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!