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

158
Vistas
Javascript : filter two array of object by id and return specific value

I'm trying to filter these two arrays of objects by their id (id & userId).

I want to return only the title where id and userId are equals

Thank you !

const usersData = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
  }
]

const albumsData = [
  {
    "userId": 1,
    "id": 1,
    "title": "FooBar"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "Trolololo"
  },
  {
    "userId": 3,
    "id": 3,
    "title": "omnis laborum odio"
  },
  {
    "userId": 4,
    "id": 4,
    "title": "non esse culpa molestiae omnis sed optio"
  }
]

const results = usersData.map(({ id }) =>
    albumsData.filter(({ userId }) => id === userId)
  );

console.log(results);

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your code seems fine, but will return a nested array, so just use flatMap instead of map. And if you just want the title, just chain another .map to it:

const usersData = [{"id": 1,"name": "Leanne Graham","username": "Bret","email": "Sincere@april.biz",}];
const albumsData = [{"userId": 1,"id": 1,"title": "FooBar"},{"userId": 1,"id": 2,"title": "Trolololo"},{"userId": 3,"id": 3,"title": "omnis laborum odio"},{"userId": 4,"id": 4,"title": "non esse culpa molestiae omnis sed optio"}];

const results = usersData.flatMap(({ id }) =>
    albumsData.filter(({ userId }) => id === userId)
).map(({title}) => title);

console.log(results);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const result = albumData.filter((data) => data.userId === usersData[0].id); 
about 4 years ago · Juan Pablo Isaza Denunciar

0

As has been pointed out, you'll get a nested array using map. But that might actually be desired if you add another item to the usersData array as you'll get the results grouped by index. If not, use flatMap with another map on the end to get the title.

const results = usersData.flatMap(({ id }) =>
    albumsData.filter(({ userId }) => id === userId)
  ).map(entry=>entry.title)

Yields:

["FooBar","Trolololo"]
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