I am facing an issue with two arrays that consist of objects. The first array contains restaurant objects with attributes such as name and averagePrice, while the second array has price objects (cheap, medium, expensive) with properties like label, lowEnd, and highEnd to determine pricing categories. My goal is to develop a function that iterates through the restaurants and returns the one whose average price falls within a specified price range. However, I encountered an error message:
Cannot read properties of undefined (reading: lowEnd)
This error occurs because I am not accurately targeting the array object property for the price. Can anyone assist me in identifying the correct method to target an array object property? Thank you.
Below is the code snippet provided:
const priceArray = [
cheap = {label: '$', lowEnd: 10, highEnd: 20},
medium = {label: '$$', lowEnd: 21, highEnd: 30},
expensive = {label: '$$$', lowEnd: 31, highEnd: 40},
];
const restaurants = [
McDonalds = {name: 'Mcdonalds', averagePrice: 12},
Sushi = {name: 'Sushi', averagePrice: 25},
Steak = {name: 'Steak', averagePrice: 35}
];
function showRestaurants(price) {
for (let restaurant of restaurants) {
//if the average price is cheap, log that restaurant
if (restaurant.averagePrice >= priceArray.price.lowEnd && restaurant.averagePrice < priceArray.price.highEnd)
console.log(restaurant);
}
};
showRestaurants(medium);