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

111
Visualizações
Filter array of objects by property with array of strings with an array of strings

So I have data that looks like this:

    const data = [
      { foo: ["0001|main|0002", "0001|main|0014", "0001|main|0016"] },
      { foo: ["0001|main|0014", "0001|main|0018", "0001|main|0019"] },
      { foo: []},
      { foo: ["0001|main|0001", "0001|main|0012", "0001|main|0022"] },
    ];

And I need to filter it with an array of strings that looks like this:

let selections = ["0014", "0016"];

I need to match the items with the same last for numbers only in the data, I currently have this which sort of works:

data.filter((item) => {
 if(!item.foo.length) return false;
 return selections.every((id) => item.foo.split('|')[2] === id)
});

The issue I'm having that when selecting two IDs like above it returns nothing. I was expecting the return value to be this:

[
 {foo: ["0001|main|0002", "0001|main|0014", "0001|main|0016"]},
 {foo: ["0001|main|0014", "0001|main|0018", "0001|main|0019"]},
]

I works fine when selection has only one string in it. I think its searching for items that match both strings in the selection.

Any help would be appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Since you want

["0001|main|0014", "0001|main|0018", "0001|main|0019"]

to be included, it sounds like you need at least one of the selections to match, rather than every selection to have a match. So, use .some instead of .every.

const data = [
  { foo: ["0001|main|0002", "0001|main|0014", "0001|main|0016"] },
  { foo: ["0001|main|0014", "0001|main|0018", "0001|main|0019"] },
  { foo: []},
  { foo: ["0001|main|0001", "0001|main|0012", "0001|main|0022"] },
];
const selections = ["0014", "0016"];

const result = data.filter(
  ({ foo }) => selections.some(
    sel => foo.some(
      str => str.endsWith(sel)
    )
  )
);
console.log(result);      

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem with your current solution is you are trying to use the split() method on an array and the split method is intended for strings.

I am sure there may be cleaner ways to approach this issue but here is what I came up with quickly.

You will have to first iterate through your data array, then iterate over each of item.foo's children elements. This then allows you to run a similar check to what you were doing just as a ternary operation instead that will push all the desired items to a dummy array which can then be returned as the result.

const data = [{
    foo: ["0001|main|0002", "0001|main|0014", "0001|main|0016"]
  },
  {
    foo: ["0001|main|0014", "0001|main|0018", "0001|main|0019"]
  },
  {
    foo: []
  },
  {
    foo: ["0001|main|0001", "0001|main|0012", "0001|main|0022"]
  },
];

let selections = ["0014", "0016"];


const filteredItems = [];

data.forEach((item) => {
  item.foo.forEach(fooItem => {
    if (!fooItem.length) return false;
    selections.every((id) => fooItem.split('|')[2] === id ? filteredItems.push(item.foo) : false);
  })
});


console.log(filteredItems)

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