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

162
Vistas
¿Cómo filtrar una matriz de objetos según la ID?

Tengo la siguiente matriz de objetos, quiero recorrerla y filtrar según la identificación o el código que paso a la función.

 state.products = [{id = 1, name = Bottle},{id = 2, name = umbrella}, {id = 3, name = shoe}] const getVal = (id)=> { const av = state.products.filter((s) s.productId == id) }

¿Lo siguiente no parece iterar a través de la matriz y verificar cada objeto? Obtengo una matriz vacía para console.log (av)

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Está en el camino correcto, pero no está utilizando Array.filter correctamente.

Y también la matriz de objetos proporcionada no está en un buen formato. El objeto debe estar en un par key:value .

 state.products = [ { id: 1, name: "Bottle" }, { id: 2, name: "umbrella" }, { id: 3, name: "shoe" } ] const getAllArrayExcept = (id) => { // this will return all the array except provided id return state.products.filter((s) => s.id !== id) } const getOnlyArray = (id) => { // this will return only item which match the provided id return state.products.filter((s) => s.id === id) } console.log(getAllArrayExcept(1)) /* output: [{ id = 2, name = umbrella }, { id = 3, name = shoe }] */ console.log(getOnlyArray(1)) /* output: [{ id = 1, name = Bottle }] */

Aquí está el fragmento de trabajo:

 const products = [ { id: 1, name: "Bottle" }, { id: 2, name: "umbrella" }, { id: 3, name: "shoe" } ] const getAllArrayExcept = (id) => { // this will return all the array except provided id return products.filter((s) => s.id !== id) } const getOnlyArray = (id) => { // this will return only item which match the provided id return products.filter((s) => s.id === id) } console.log("All array except: ", getAllArrayExcept(1)) console.log("Only provided item in array: ", getOnlyArray(1))

about 4 years ago · Santiago Trujillo 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