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

323
Views
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 answers
Answer question

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 Report

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 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!