Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda