0

`Dear guys, i am desperately wondering why my filter function does not work while sort function works well.

code is as below,

const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';

let cities = [];

fetch (endpoint)
.then (jsonout => jsonout.json())
.then(pushdata => cities.push(...pushdata));

let sortgo = cities.sort((a,b) => {
let firstone = a.population;
let nextone = b.population;

firstone > nextone ? -1 : 1;
});

let gofilter = cities.filter(n => n.population > 100000);

with the code above, when i do console.table(sortgo) in browser(f12), it shows cities in the order of population very well.

but when i console.table(gofilter) , it just returns undefined.. seeking for your help on this, thank you.

*attached is how json data looks like.enter image description here

I was trying to filter json data whose population is over 100000.`

  • sorry i deleted backslashes. (it was my first time to upload question here, so i had kind of trouble in uploading..) – Crying_super_noob May 27 '23 at 10:58
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – gre_gor May 27 '23 at 11:34
  • `filter` never returns `undefined`. – gre_gor May 27 '23 at 11:41
  • Actually, if it was an async issue, both would be just empty arrays. With async issue fixed, [the problem is not reproducible](https://jsfiddle.net/zobp3k1w/). – gre_gor May 27 '23 at 11:49
  • to understand How do I return the response from an asynchronous call? <-- this one, maybe my knowledge is not enough to understand,, yesterday I got some answer from question but somehow it has been deleted maybe due to duplicate question. – Crying_super_noob May 28 '23 at 10:16

0 Answers0