I need to loop through an array of objects to determine if specific criteria are met. Let's consider the structure of my array:
const arrayOfItems = [
{
delivery_method: {
delivery_method: 'car',
delivery_rate: 1,
pickup_day: 'none',
},
total_cost: 5,
items: [{}],
},
{
delivery_method: {
delivery_method: 'pickup',
pickup_day: 'T',
delivery_rate: 0,
},
total_cost: 5,
items: [{}],
},
]
Currently, I have a checkMethodChosen function that uses the following logic:
async checkMethodChosen() {
let check = await arrayOfItems.map((item) => {
if (
(item.delivery_method.delivery_method === 'pickup'
&& item.delivery_method.pickup_day !== 'none')
|| item.delivery_method.delivery_method === 'car'
|| item.delivery_method.delivery_method === 'bike'
) {
return false
}
return true
})
let deliveryChosen = check.includes(false)
this.setState({
deliveryChosen,
})
}
This function updates the state based on whether 'delivery_method' is set to 'pickup' with a selected pickup_day or if it's 'car' or 'bike'. It works well when there's only one object in the array but has issues with multiple objects.
The desired functionality is for this.state.deliveryChosen to be true only if 'delivery_method' is chosen in every object. If any object lacks this selection, then this.state.deliveryChosen should be false.
Appreciate your help!