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

82
Visualizações
Problem with logging things from an array

i have a problem with logging something from an array. I need to find the highest rated locations from each place and i need to log the name and coordinates from those 3 locations. I have been stuck here for a while now anyone know how to do this?

this is the code i already have:

let locaties = [
    {naam: 'locatie1', type: 'cafe', rating: 8, coordinaat: {lat: 17, lon: 3},},
    {naam: 'locatie2', type: 'winkel', rating: 3, coordinaat: {lat: 23, lon: 9},},
    {naam: 'locatie3', type: 'Restaurant', rating: 7, coordinaat: {lat: 3, lon: 17},},
    {naam: 'locatie4', type: 'winkel', rating: 7, cordinaat: {lat: 20, lon: 10},},
    {naam: 'locatie5', type: 'cafe', rating: 1, coordinaat: {lat: 12, lon: 13},},
    {naam: 'locatie6', type: 'winkel', rating: 5, coordinaat: {lat: 13, lon: 2},},
    {naam: 'locatie7', type: 'Restaurant', rating: 6, coordinaat: {lat: 7, lon: 17},},
    {naam: 'locatie8', type: 'Restaurant', rating: 2, cordinaat: {lat: 3, lon: 15},},
    {naam: 'locatie9', type: 'cafe', rating: 4, coordinaat: {lat: 30, lon: 12},},
    {naam: 'locatie10', type: 'winkel', rating: 9, cordinaat: {lat: 27, lon: 19},},
];

Object.keys(locaties).forEach(key => {
    console.log(key);
    console.log(locaties[key].naam);
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

let locations = [{
    name: 'locatie1',
    type: 'cafe',
    rating: 8,
    coordinate: {
      lat: 17,
      lon: 3
    },
  },
  {
    name: 'locatie2',
    type: 'winkel',
    rating: 3,
    coordinate: {
      lat: 23,
      lon: 9
    },
  },
  {
    name: 'locatie3',
    type: 'Restaurant',
    rating: 7,
    coordinate: {
      lat: 3,
      lon: 17
    },
  },
  {
    name: 'locatie4',
    type: 'winkel',
    rating: 7,
    coordinate: {
      lat: 20,
      lon: 10
    },
  },
  {
    name: 'locatie5',
    type: 'cafe',
    rating: 1,
    coordinate: {
      lat: 12,
      lon: 13
    },
  },
  {
    name: 'locatie6',
    type: 'winkel',
    rating: 5,
    coordinate: {
      lat: 13,
      lon: 2
    },
  },
  {
    name: 'locatie7',
    type: 'Restaurant',
    rating: 6,
    coordinate: {
      lat: 7,
      lon: 17
    },
  },
  {
    name: 'locatie8',
    type: 'Restaurant',
    rating: 2,
    coordinate: {
      lat: 3,
      lon: 15
    },
  },
  {
    name: 'locatie9',
    type: 'cafe',
    rating: 4,
    coordinate: {
      lat: 30,
      lon: 12
    },
  },
  {
    name: 'locatie10',
    type: 'winkel',
    rating: 9,
    coordinate: {
      lat: 27,
      lon: 19
    },
  },
];

highest_rated_locations = locations
  .sort((a, b) => b.rating - a.rating) // order elements by rating descending. Note: the original locations array changes, since the sort method is mutable, if the original array shouldn't be changed, use Array.from
  .slice(0, 3) // return the first three elements of the array

for (const location of highest_rated_locations) {
  console.log(`Name: ${location.name} | Lattitude: ${location.coordinate.lat} | Longitude: ${location.coordinate.lon}`)
}

I adjusted some names in the code for better understanding. For some explanation, Object.keys() returns all the keys of the given Object. In your case this is an array and the values will be 0, 1, 2, .... If you would do this with one of the objects inside the array, for example Object.keys(locations[0]) you would get the following: name, type, rating, coordinate. lat and lan won't be in there since those are nested properties of coordinate. I would suggest to you, that you inform yourself a bit about javascript loops, since they are pretty important.

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