Tengo dos matrices compuestas de objetos. Una matriz está formada por objetos de restaurante con nombre de propiedades y precio promedio. La otra matriz está formada por objetos de precio (barato, medio, caro) con etiqueta de propiedades, lowEnd y highEnd. lowEnd y highEnd representan los umbrales que determinan si el restaurante es barato, medio o caro. Quiero crear una función que recorra los restaurantes y devuelva el restaurante cuyo precio promedio cae dentro del rango de precios dado. Me sale un error que dice:
No se pueden leer las propiedades de undefined (lectura: lowEnd)
Sé que esto se debe a que no estoy apuntando correctamente a la propiedad del objeto de matriz del precio. ¿Alguien puede ayudarme a descubrir cómo apuntar correctamente a una propiedad de objeto de matriz? Gracias
Aquí está mi código
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);No tiene priceArray.price y priceArray también es una matriz con nombres clave cheap , medium y expensive
Ya pasó medium como los datos que desea verificar correctamente, por lo que le sugiero que use el parámetro de price en showRestaurants(price) en su lugar. En este caso, considerará medium como price .
showRestaurants(medium);La posible solución podría ser
priceArray.price.lowEnd a price.lowEndpriceArray.price.highEnd a price.highEnd 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 >= price.lowEnd && restaurant.averagePrice < price.highEnd) console.log(restaurant); } }; showRestaurants(medium); .as-console-wrapper { max-height: 100% !important; top: 0; }puedes hacer algo como esto
const priceArray = [ {label: '$', lowEnd: 10, highEnd: 20}, {label: '$$', lowEnd: 21, highEnd: 30}, {label: '$$$', lowEnd: 31, highEnd: 40}, ]; const restaurants = [ {name: 'Mcdonalds', averagePrice: 12}, {name: 'Sushi', averagePrice: 25}, {name: 'Steak', averagePrice: 35} ]; const withPriceLabel = restaurants.map(r => { return { ...r, priceEvaluation: priceArray.find(p => r.averagePrice >= p.lowEnd && r.averagePrice <= p.highEnd).label } }) console.log(withPriceLabel.filter(r => r.priceEvaluation === '$'))Mella. Aquí hay una solución rápida
const CHEAP = { label: "$", lowEnd: 10, highEnd: 20 }; const MEDIUM = { label: "$$", lowEnd: 21, highEnd: 30 }; const EXPENSIVE = { label: "$$$", lowEnd: 31, highEnd: 40 }; const priceArray = [CHEAP, MEDIUM, EXPENSIVE]; const McDonalds = { name: "Mcdonalds", averagePrice: 12 }; const Sushi = { name: "Sushi", averagePrice: 25 }; const Steak = { name: "Steak", averagePrice: 35 }; const restaurants = [McDonalds, Sushi, Steak]; function showRestaurants(price) { const filteredRestaurants = []; //filtering the category const categories = priceArray.filter((level) => { return level.lowEnd < price && price < level.highEnd; }); ////filtering the restaurants restaurants.map((restaurant) => { categories.map((category) => { if ( category.lowEnd < restaurant.averagePrice && restaurant.averagePrice < category.highEnd ) { filteredRestaurants.push(restaurant); } }); }); filteredRestaurants.map((restaurant) => console.log(restaurant.name)); } showRestaurants(15);