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

144
Views
Check if ID Is Found Another Array in ES6

I wanted to filter out the data. I wanted to check if data on data1 is found on data2 and to check if it has errorMessages. Please check my code below. Is there a better way to do it?

data1

[
    {
        "ids": "0111",  
    },
    {
        "ids": "0222",
    },
    {
         "ids": "0333",
    }
]

data2

[
  {
    "id": "0111",
    "errorMessages": [
      {
        "message": ["sample error message 1"]
      }
    ]
  },
  {
    "id": "0333",
    "errorMessages": []
  }
]

Code

const output= data1.filter(
  (element) => element.ids === data2.find((data) => data).id
);

console.log("output", output);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

.find((data) => data) doesn't do anything useful - each item in the array is an object, which is truthy, so that'll always return the first element in the array.

If you did want to .find a matching element in the other array - then a better approach would be to make a Set of the IDs found in the other array first (Set lookup is much quicker - O(1) - than .find, which is O(n)).

You also need to implement the logic to check if the errorMessages is empty.

const data1 = [
    {
        "ids": "0111",  
    },
    {
        "ids": "0222",
    },
    {
         "ids": "0333",
    }
]
const data2 = [
  {
    "id": "0111",
    "errorMessages": [
      {
        "message": ["sample error message 1"]
      }
    ]
  },
  {
    "id": "0333",
    "errorMessages": []
  }
]


const ids = new Set(
  data2
    .filter(item => item?.errorMessages.length)
    .map(item => item.id)
);

const output= data1.filter(
  element => ids.has(element.ids)
);
console.log("output", output);

about 4 years ago · Juan Pablo Isaza Report

0

Without Set, but use Object as the map.


const IDKeys = {};

data2.forEach(data => {
    if (data.errorMessages.length){
       IDKeys[data.id] = true; // means valid 
    }
})


const filteredArray = data1.filter(data => IDKeys[data.id]);

This would be only O(n) since accessing key on object is O(1)

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!