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

105
Vistas
JavaScript filter should return arrays if the id match

I have books data. I'd like to return the books if they match the author's id. When I try to filter out data that does not match my 'author's' id. However, it always returns all book data.

const author = "AUTHOR#e9bb9d29-7f20-4fce-892c-6a155dbee42c";

const Book = [
  {
    publishingYear: "2020",
    rating: 5.2,
    GSI1SK: "AUTHOR#a731ea70-f3f3-4811-9734-f22c0856385d",
    genre: ["adventure", "drama", "scifi"],
    GSI1PK: "AUTHOR",
    page: 100,
    publisher: "Afternoon pub",
    SK: "BOOK#c4a58f20-4977-4db8-9723-0185f68cdf01",
    price: "3.50",
    PK: "BOOKS",
    author: "Krishna",
    title: "Me and mySelf"
  },
  {
    publishingYear: "2020",
    rating: 5.2,
    GSI1SK: "AUTHOR#6b7c10ff-0e2c-46bd-9697-3b51730d8b29",
    genre: ["adventure", "drama", "scifi"],
    GSI1PK: "AUTHOR",
    page: 100,
    publisher: "Day pub",
    SK: "BOOK#e4773a32-5451-42c6-a3f1-a6aa45176256",
    price: "3.50",
    PK: "BOOKS",
    author: "John doe",
    title: "Hello world"
  },
  {
    publishingYear: "2020",
    rating: 5.2,
    GSI1SK: "AUTHOR#a731ea70-f3f3-4811-9734-f22c0856385d",
    genre: ["adventure", "drama", "scifi"],
    GSI1PK: "AUTHOR",
    page: 100,
    publisher: "Night Pub",
    SK: "BOOK#fb56a876-41bc-49f9-9762-c48e90af3117",
    price: "3.50",
    PK: "BOOKS",
    author: "Krishna",
    title: "Amazing Race"
  }
];

const Books = Book.filter((i) => {
  console.log(i.GSI1SK);
  i.GSI1SK === author;
  return i;
});

console.log(Books);

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

0

You are using filter the wrong way, you should return true or false based on the condition you're matching,

const Books = Book.filter((i) => {
  console.log(i.GSI1SK);
  return i.GSI1SK === author;
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

And you can omit unnecessary lines. Try this.

const Books = Book.filter(i => i.GSI1SK === author)
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