Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
Looping through objects in arrays to compare values

I have two arrays composed of objects. One array is made of restaurant objects with properties name and averagePrice. The other array is made of price objects (cheap, medium, expensive) with properties label, lowEnd, and highEnd. lowEnd and highEnd represent the thresholds that determine if the restaurant is cheap, medium, or expensive. I want to create a function that loops through the restaurants, and returns the restaurant whose average price lands within the given price range. I get an error that says:

Cannot read properties of undefined (reading: lowEnd)

I know that this is because I am not properly targeting the array object property of the price. Can someone help me figure out how to properly target an array object property? Thanks

Here is my code

    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);
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You don't have priceArray.price and priceArray is also an array with key names cheap, medium, and expensive

You've already passed medium as the data you want to check correctly, so I'd suggest you should use price param in showRestaurants(price) instead. In this case, it will consider medium as price

showRestaurants(medium);

The possible fix could be

  • priceArray.price.lowEnd to price.lowEnd
  • priceArray.price.highEnd to 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; }

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this

 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 === '$'))

about 4 years ago · Juan Pablo Isaza Relatório

0

Nick. Here's a quick solution

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);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda