Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda