1

There is an array of weather for 5 days of how many countries each country is divided into every three hours and I have to take out the city with the maximum temperature (temp_max) of each day how to do it in the most efficient and short way with JavaScript?

I would love a quick reply Thank you very much!

Example of one object (one city) an array of several such objects I will explain myself: I need to take out according to the same dt number that in this city as well as in the other cities the city with max of main.temp_max when I go through an array that has 10 cities like this city This is the Array:

   []
0:
city: {id: 281184, name: "Jerusalem", coord: {…}, country: "IL", population: 714000, …}
cnt: 40
cod: "200"
list: Array(40)
0:
clouds: {all: 0}
dt: 1626382800
dt_txt: "2021-07-15 21:00:00"
main: {temp: 296.34, feels_like: 296.65, temp_min: 296.34, temp_max: 296.37, pressure: 1003, …}
pop: 0
sys: {pod: "n"}
visibility: 10000
weather: [{…}]
wind: {speed: 3.08, deg: 274, gust: 3.89}
__proto__: Object
1: {dt: 1626393600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
2: {dt: 1626404400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
3: {dt: 1626415200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
4: {dt: 1626426000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
5: {dt: 1626436800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
6: {dt: 1626447600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
7: {dt: 1626458400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
8: {dt: 1626469200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
9: {dt: 1626480000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
10: {dt: 1626490800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
11: {dt: 1626501600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
12: {dt: 1626512400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
13: {dt: 1626523200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
14: {dt: 1626534000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
15: {dt: 1626544800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
16: {dt: 1626555600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
17: {dt: 1626566400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
18: {dt: 1626577200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
19: {dt: 1626588000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
20: {dt: 1626598800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
21: {dt: 1626609600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
22: {dt: 1626620400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
23: {dt: 1626631200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
24: {dt: 1626642000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
25: {dt: 1626652800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
26: {dt: 1626663600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
27: {dt: 1626674400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
28: {dt: 1626685200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
29: {dt: 1626696000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
30: {dt: 1626706800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
31: {dt: 1626717600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
32: {dt: 1626728400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
33: {dt: 1626739200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
34: {dt: 1626750000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
35: {dt: 1626760800, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
36: {dt: 1626771600, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
37: {dt: 1626782400, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
38: {dt: 1626793200, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
39: {dt: 1626804000, main: {…}, weather: Array(1), clouds: {…}, wind: {…}, …}
length: 40
__proto__: Array(0)
message: 0
__proto__: Object
1: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
2: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
3: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
4: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
5: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
6: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
7: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
8: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
9: {cod: "200", message: 0, cnt: 40, list: Array(40), city: {…}}
length: 10
Oren A
  • 21
  • 4
  • 2
    It would be helpful to provide a valid javascript object so we can see what you are actually working with – kigiri Jul 15 '21 at 15:04
  • 1
    Does this answer your question? [Finding the max value of an attribute in an array of objects](https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects) – GalAbra Jul 15 '21 at 15:14
  • Hi Kigiri, I have expanded the explanation and you will see if it is more clear to you... – Oren A Jul 15 '21 at 17:35
  • Hi GalAbra, I have seen this explanation it does not answer me at all the case is much more complicated. – Oren A Jul 15 '21 at 17:37
  • i hate to be this guy but what code did you try and what were the results? – The Bomb Squad Jul 15 '21 at 19:39
  • I try to .... .... – Oren A Jul 16 '21 at 07:15

1 Answers1

0

An idea is to sort it in order of temp_max(for highest value to be in 0 position) then using shift that will take away that object from the array

However I'm not sure if this is what you want since the wording of the question is kind of confusing ;-;

function cutOutHighTemp(arr){
  return [...arr] //copy of arr(cuz y not)
  .sort((a,b)=>b.main.temp_max - a.main.temp_max)
  .shift() //take out 0 position(which has highest temp_max)
}
//example usage
var editedWeather=cutOutHighTemp(weather)
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17
  • There are three problems: The one I need to take the highest temperature of each date separately. And the second problem is that every city has an array of objects Containing all the dates. The third is that I need to know at the end both the temperature and the date and the name of the city. This means that I need to first know on each date of each city the highest heat and then check what is the highest of all the cities on each date separately and I need at the end the name of the city and the date and temperature ........... – Oren A Jul 15 '21 at 22:00