Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!