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

179
Visualizações
Basic Javascript map, filter question in array

I want to get things with collectionId that match those in the list. In other words, I want to get the desired result. How can I do it?? please help me....

const music_list = {
  "resultCount": 50,
  "results": [{
      "collectionId": 123,
      "ArtistId": "BLACKPINK"
    },
    {
      "collectionId": 456,
      "ArtistId": "BTS"
    },
    {
      "collectionId": 789,
      "ArtistId": "CARDIBI"
    }
  ]
}
const list = [123, 142, 118];

In this situation, Desired result = [{"collectionId":123, "ArtistId": "BLACKPINK"}]

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

0

You can map the list to the relevant objects - for each ID, you will try to find the corresponding object in the results array. At the end, you can filter out the missing elements.

const result = list
  .map(id => music_list.results.find(item => item.collectionId === id))
  .filter(item => item)

How it works: music_list.results.find(item => item.collectionId === 123) would return the item in music_list.results that has collectionId: 123 (or undefined if no such item exists). Furthermore, list.map(id => ...something...) would apply the ...something... to each id in the list and return a new array with the results. So here we map the IDs to the matching objects in your results array. At the end, we use filter to remove elements that are falsy (undefined is falsy), i.e. remove the missing elements from the result (without that, it would return something like [{ ... }, undefined, undefined]).

You can see it in action here:

const music_list = {
  "resultCount": 50,
  "results": [
    {
      "collectionId": 123,
      "ArtistId": "BLACKPINK"
    },
    {
      "collectionId": 456,
      "ArtistId": "BTS"
    },
    {
    "collectionId": 789,
    "ArtistId": "CARDIBI"
    }
  ]
}

const list = [123, 142, 118];

const result = list
  .map(id => music_list.results.find(item => item.collectionId === id))
  .filter(item => item);

console.log(result);

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