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

167
Views
Filtering objects that has unique combination of values

I've got an array of objects that look like this:

[{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"}]

From the objects above, there's some duplicate account names, but they're unique due to the account type. I'm trying to figure out a way to filter it so that a combination of key values would make an object unique, rather than a single key. Something like filtering the Account Name && Account Type.

I've also noticed some solutions would create a new array and include only the keys to filter, but I'd like the solution to preserve all keys from an object rather than creating a new object with lesser keys.

Expected result would look like this:

[{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd}]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const array = [{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},
{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"},
{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"}]

const filteredArr = array.reduce((acc, current) => {
  const x = acc.find(item => (item['Account Name'] === current['Account Name'] ) && (item['Account Type'] === current['Account Type']));
  if (!x) {
    return acc.concat([current]);
  } else {
    return acc;
  }
}, []);

console.log(filteredArr);

about 4 years ago · Juan Pablo Isaza Report

0

Use a Map. Loop over the array and on each iteration create a key from the Account Name and Account Type, and set the whole object as its value. Finally use values() to return a deduped array.

const data=[{"Account Name":"John Hill","Account Type":"Services",Organization:"A","Account Address":"2 Westall Rd"},{"Account Name":"John Hill","Account Type":"Training",Organization:"A","Account Address":"2 Westall Rd"},{"Account Name":"Dave Butcher","Account Type":"Engieering",Organization:"A","Account Address":"Level 1/55 Sunshine Blvd"},{"Account Name":"Jake Wellington","Account Type":"Management",Organization:"A","Account Address":"11 Maroochy Rd"},{"Account Name":"Jake Wellington","Account Type":"Management",Organization:"A","Account Address":"11 Maroochy Rd"}];

const map = new Map();

for (const obj of data) {
  const key = `${obj['Account Name']}-${obj['Account Type']}`;
  map.set(key, obj);
}

console.log([...map.values()]);

about 4 years ago · Juan Pablo Isaza Report

0

You can use the hash group approach to filter by several properties:

const data = [{"Account Name":"John Hill","Account Type":"Services","Organization":"A","Account Address":"2 Westall Rd"},{"Account Name":"John Hill","Account Type":"Training","Organization":"A","Account Address":"2 Westall Rd"},{"Account Name":"Dave Butcher","Account Type":"Engieering","Organization":"A","Account Address":"Level 1/55 Sunshine Blvd"},{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"},{"Account Name":"Jake Wellington","Account Type":"Management","Organization":"A","Account Address":"11 Maroochy Rd"}];

const uniqHashes = data.reduce((acc, item) => {
    const hash = [item['Account Name'], item['Account Type']].join('-');
    acc[hash] ??= item
    return acc;
}, {});

const uniqs = Object.values(uniqHashes);

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

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!