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

66
Visualizações
Return only the matched objects from multiple strings in a multidimensional array using Javascript

I currently have 2 arrays. One is a flat array containing multiple strings and the other is a nested array. I am trying to return my nested array with only the objects that match the strings from my first array. What I am currently running into is my whole array is being returned if there is just one match oppose to just the matched object.

JS:

const rootIds = ["18828861", "15786287", "13435676"]
const filtData = [
    {
      year: "2022",
      awards: [
        {
          list: [
            {
              rootId: "18828861",
              name: "Globe",
            },
            {
              rootId: "15786222",
              name: "Golden"
            },
          ],
        },
      ],
    },
    {
      year: "2021",
      awards: [
        {
          list: [
            {
              rootId: "15786223",
              name: "Car",
            },
            {
              rootId: "13435676",
              name: "Red",
            },
          ],
        },
        {
          list: [
            {
              rootId: "15786287",
              name: "Black",
            },
            {
              rootId: "1578622",
              name: "Blue",
            },
          ],
        },
      ],
    },
];
const res = rootIds.map((a) => a);
const findIds = filtData.map(obj => obj.awards.filter(( { list }) => list.find(o => rootIds.includes(o.rootId))));
console.log("filtData====", findIds)

Desired output would be:

"filtData====", [[{
  list: [{
  name: "Globe",
  rootId: "18828861"
}]
}], [{
  list: [{
  name: "Red",
  rootId: "13435676"
}]
}, {
  list: [{
  name: "Black",
  rootId: "15786287"
}]
}]]

JsFiddle of not working demo: https://jsfiddle.net/hb7ej5sg/

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

0

Found the cause at:

const findIds = filtData.map(obj => obj.awards.filter(( { list }) => list.find(o => rootIds.includes(o.rootId))));

Since you are using #filter on obj.awards array instead of #map and #find on list instead of #filter, you are getting the whole array from obj.awards[].list.

Using correct methods as follow would fix the issue:

const findIds = filtData.map(obj =>
    obj.awards.map(( { list }) =>
        list.filter(o => rootIds.includes(o.rootId))
    )
);
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