Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda