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

320
Views
¿Por qué .includes() no funciona en mi matriz de objetos?

Estoy tratando de filtrar una matriz de objetos anidados con un resultado deficiente.

.includes no encontrará mi elemento de búsqueda aunque esté presente en la matriz. Estaría agradecido por su ayuda!

 const date = { delivery_date: '2021-10-07' }; const customers = [ { first_name: 'Doris' , orders: [ { product: { name: 'White shell', price: 10, schedule: [ { delivery_date: '2021-10-07' } , { delivery_date: '2021-10-14' } ] } } , { product: { name: 'Pink shell', price: 15, schedule: [ { delivery_date: '2021-10-07' } ] } } ] } , { first_name: 'Nemo' , orders: [ { product: { name: 'Carbonated water', price: 10, schedule: [ { delivery_date: '2021-10-07' } , { delivery_date: '2021-10-14' } ] } } ] } ]; const res = customers.filter(function(customer) { return customer.orders.some(function(order) { console.log(date, order.product.schedule, order.product.schedule.includes(date)); return order.product.schedule.includes(date); }); }); console.log(res);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

En lugar de buscar si la matriz de schedule contiene date , busque un elemento de schedule que tenga la misma delivery_date que date .

 const date = { delivery_date: '2021-10-07' }; const customers = [ { first_name: 'Doris' , orders: [ { product: { name: 'White shell', price: 10, schedule: [ { delivery_date: '2021-10-07' } , { delivery_date: '2021-10-14' } ] } } , { product: { name: 'Pink shell', price: 15, schedule: [ { delivery_date: '2021-10-07' } ] } } ] } , { first_name: 'Nemo' , orders: [ { product: { name: 'Carbonated water', price: 10, schedule: [ { delivery_date: '2021-10-07' } , { delivery_date: '2021-10-14' } ] } } ] } ]; const res = customers.filter(function(customer) { return customer.orders.some(function(order) { return order.product.schedule.find(schedule => schedule.delivery_date === date.delivery_date) !== undefined; // alternative : return !!(order.product.schedule.find(schedule => schedule.delivery_date === date.delivery_date)); }); }); console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar un Array.map() en delivery_date para eso.
no estabas tan lejos

 const date = { delivery_date: '2021-10-07' }; const customers = [ { first_name: 'Doris' , orders: [ { product: { name: 'White shell', price: 10, schedule: [ { delivery_date: '2021-10-07' } , { delivery_date: '2021-10-14' } ] } } , { product: { name: 'Pink shell', price: 15, schedule: [ { delivery_date: '2021-10-07' } ] } } ] } , { first_name: 'Nemo' , orders: [ { product: { name: 'Carbonated water', price: 10, schedule: [ { delivery_date: '2021-10-07' } , { delivery_date: '2021-10-14' } ] } } ] } ]; const res = customers.filter(({orders}) => orders.some( ({product}) => product.schedule.map(({delivery_date}) => delivery_date) .includes(date.delivery_date) ) ) console.log( res )
 .as-console-wrapper { max-height: 100% !important; top: 0 }

para mostrar lo que hace product.schedule.map()
(como se preguntó aquí en un comentario)

 const res = customers.filter(({orders}) => orders.some( ({product}) => { let tmp_array = product.schedule.map(({delivery_date}) => delivery_date) console.log(product.name, JSON.stringify(tmp_array) ) return tmp_array.includes(date.delivery_date) } ) )

regresará

 White shell ["2021-10-07","2021-10-14"] Carbonated water ["2021-10-07","2021-10-14"]

este no se muestra:

 Pink shell ["2021-10-07"]

porque White shell ya contiene su fecha delivery_date ( 2021-10-07 ) y order.some() no necesita probar la siguiente matriz product.schedule .

si quieres verlo,
simplemente cambie sus datos de 'White shell'--> '2021-10-07'
a 'White shell'--> '2000-01-01'

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!