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

94
Views
Filtering two arrays based on another array values and also push the unavailable values into another array

I have 2 arrays one is "searchEachIndex" and another one is "data".

Based on "searchEachIndex" array i want to filter the "data" array. i.e,

Here in searchEachIndex array i have 123 value that matches with data array's employeId.

should push the object into res array here { id: 2, employeId: 123 }, { id: 3, employeId: 123 }, If both values matches. otherwise push the mismatching searchEachIndex values here 1234, 12 into another array i.e, noData array.

    let searchEachIndex = [123, 1234, 12]

    let data = [
    {
      id: 1,
      employeId: 121
    },
    {
      id: 2,
      employeId: 123
    },
    {
      id: 3,
      employeId: 123
     }
    ];

    let res=[];

    let noData = [];

    searchEachIndex.forEach(val => {
       data.map(item => {
          if(item.employeId === val) {
           res.push(item)
          }
       })
    })


    console.log('Both emp id and searchEachIndex value matched array', res);

    consolelog('Ids not found array', noData);

Actually i need response like the following : res : [{ id: 2, employeId: 123 }, { id: 3, employeId: 123 }] and noData : [1234, 12]

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

0

You can substitute the .map with .forEach since you do not require a transformed array returned and are only looking for a for loop.

Also you can keep a boolean flag which lets you know if the current val had a corresponding entry in the data array. That way you can push (/not push) into your noData array.

Building up on your code :

let searchEachIndex = [123, 1234, 12]

let data = [
{
  id: 1,
  employeId: 121
},
{
  id: 2,
  employeId: 123
},
{
  id: 3,
  employeId: 123
 }
];

let res=[];

let noData = [];

searchEachIndex.forEach(val => {
   let found = false;
   data.forEach(item => {
      if(item.employeId === val) {
       found = true;
       res.push(item)
      }
   })
   if(!found) noData.push(val);
})

console.log('Both emp id and searchEachIndex value matched array', res);

console.log('Ids not found array', noData);

about 4 years ago · Juan Pablo Isaza Report

0

You could take an object to keep track of used values and filter the arrays.

const
    search = [123, 1234, 12],
    used = Object.fromEntries(search.map(k => [k, false])),
    data = [{ id: 1, employeId: 121 }, { id: 2, employeId: 123 }, { id: 3, employeId: 123 }],
    result = data.filter(({ employeId }) => {
        if (employeId in used) return used[employeId] = true;
    }),
    noData = search.filter(k => !used[k]);

console.log(noData);
console.log(result);
.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!