Reputation: 261
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);
Upvotes: 0
Views: 509
Reputation: 22274
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'
Upvotes: 1
Reputation: 2384
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);
Upvotes: 2