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

322
Vistas
Why .includes() is not working on my object array?

I'm trying to filter a nested object array with poor result.

.includes won't find my search element even though it is present in the array. I would be grateful for your help!

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 Respuestas
Responde la pregunta

0

Instead of searching if the schedule array contains date, search for an element of schedule that have the same delivery_date as 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 Denunciar

0

You may use an Array.map() on delivery_date for that.
You weren't that far

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 }

to show what product.schedule.map() do
(as asked in here in a comment)

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)
    }
  ) )

will return

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

this one is not showed:

Pink shell ["2021-10-07"]

because White shell allready contain your delivery_date (2021-10-07) and the order.some() doesn't need to test the next product.schedule array.

if you want to see it,
just change your data of 'White shell'--> '2021-10-07'
to 'White shell'--> '2000-01-01'

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