Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Filtrar matriz de objetos en los que no todas las claves están presentes - JavaScript

Tengo una serie de objetos:

 [ { "id": "1", "LB1": "Kod produktu: PMPSB216IN80", "Type": "Figurki" }, { "id": "2", "Type": "Głowa Do Stylizacji" }, { "id": "3", "LB1": "Kod produktu: 97834", "Type": "Pluszak" }, ]

Entonces estoy definiendo una cadena: '97834'

Y quiero filtrar la matriz según el valor LB1:

 filteredProducts = productList.filter(product => product.LB1.replace('Kod produktu: ', '').toLowerCase() === this.props.searchTerm.toLowerCase());

Sin embargo, como puede ver arriba, no todos los objetos contienen este LB1, y la función arroja un error:

 Test.js:101 Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Verifique si existe LB1; si no es así, fíltrelo como si no coincidiera con su término de búsqueda (aquí estoy usando AND lógico :

 filteredProducts = productList.filter(product => product.LB1 != null && product.LB1.replace('Kod produktu: ', '').toLowerCase() === this.props.searchTerm.toLowerCase());
about 4 years ago · Juan Pablo Isaza Report

0

Podría agregar una condición, en su función de filtro, que verifique la existencia de esa propiedad. Aquí, estoy usando el operador in para validar eso.

 const productList = [ { "id": "1", "LB1": "Kod produktu: PMPSB216IN80", "Type": "Figurki" }, { "id": "2", "Type": "Głowa Do Stylizacji" }, { "id": "3", "LB1": "Kod produktu: 97834", "Type": "Pluszak" }, ] const searchTerm = '97834' /* this.props.searchTerm.toLowerCase() */; const filteredProducts = productList.filter(product => 'LB1' in product && product.LB1.replace('Kod produktu: ', '').toLowerCase() === searchTerm ); console.log('output', filteredProducts);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!