Looking to see if an order line_item has been refunded before processing…
Here is a single order:
var order = {
line_items: [
{
id: 1326167752753
}
],
refunds: [
{
refund_line_items: [
{
id: 41264152625,
line_item_id: 1326167752753,
}
]
}
]
};
Trying to log out the filter results:
console.log(
_.filter(order, {
refunds: [
{
refund_line_items: [
{
line_item_id: 1326167752753
}
]
}
]
}).length
);
I’m getting 0
on the console.
Am I using _.filter wrong in this case?
2
Answers
Function take needs an array (
order
is not an array,order.refunds
is) and a predicate, not an object.Anyway, I’d write it using
Array.some
:Or, alternatively, getting all
line_item_id
s and checking inclusion:You can use
some
andfind
and do this in lodash and also easily in ES6: