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

203
Views
How to filter array of objects on multiple conditions and add exception?

I currently have an array of objects that is filtered based on unique IDs and unique Names being together:

Initial input:

const data = [
  {
    id: 1234,
    name: "Name1"
  },
  {
    id: 1234,
    name: "Name1"
  },
  {
    id: 1234,
    name: "Name2"
  },
  {
    id: 1234,
    name: null
  },
  {
    id: 5678,
    name: "Name3"
  },
  {
    id: 5678,
    name: "Name3"
  },
  {
    id: 5678,
    name: null
  },
  {
    id: 9999,
    name: null
  },
  {
    id: 9999,
    name: null
  },
];
data.filter((value, index, self) => {
      return (
        self.findIndex(
          (v) =>
            v.id === value.id &&
            v.name=== value.name
        ) === index
      );
});
  [
      {
        id: 1234
        name: "Name1"
      },
      {
        id: 1234
        name: "Name2"
      },
      {
        id: 1234
        name: null
      },
      {
        id: 5678
        name: null
      },
      {
        id: 5678
        name: "Name3"
      },
      {
        id: 9999
        name: null
      },
  ]

However, some Ids do not have any duplicates and I want to remove all the null name associated objects EXCEPT where the id is not a duplicate and to look like the following:

  [
      {
        id: 1234
        name: "Name1"
      },
      {
        id: 1234
        name: "Name2"
      },
      {
        id: 5678
        name: "Name3"
      },
      {
        id: 9999
        name: null
      },
  ]

At the moment the duplicated Ids with null names still exist.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I kept the filtering part you implemented as an intermediate output. using that i got a count for each id. then using that i filtered out from the intermediate array where if the has count > 1 and has a null name i remove it.

 const data = [{id: 1234,name: "Name1"},{id: 1234,name: "Name1"},{id: 1234,name: "Name2"},{id: 1234,name: null},{id: 5678,name: "Name3"},{
id: 5678,name: "Name3"},{id: 5678,name: null},{id: 9999,name: null},{id: 9999,name: null},];
  
//filtering out duplicates
let a = data.filter((value, index, self) => {
      return (
        self.findIndex(
          (v) =>
            v.id === value.id &&
            v.name=== value.name
        ) === index
      );
});
    
  //get count for ids in partially filtered 
let idcount = a.reduce((acc,curr) => {
    if(!acc[curr.id])acc[curr.id] = 0
  acc[curr.id] += 1
  return acc;
},{})

//remove the duplicate nulls
let final = a.filter((el) => {
    return !(idcount[el.id]>1 && el.name===null)
})


console.log(final)

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!