After extracting an array of objects from the raw data provided in this link, I have encountered a dataset that resembles the following:
[0 ... 99] 0 : city : "New York" growth_from_2000_to_2013 : "4.8%" latitude : 40.7127837 longitude : -74.0059413 population : "8405837" rank : "1" state : "New York" proto : Object 1 : {city: "Los Angeles", growth_from_2000_to_2013: "4.8%", latitude: 34.0522342, longitude: -118.2436849, population: "3884307", ...}
This dataset is stored as const JSON_LOCS
, referenced in the code below.
I am attempting to filter this dataset to find cities that contain specific text. I've tried two different approaches. One method seems to be successful, while the other involving Array.prototype.filter()
does not seem to work.
const test = [];
for (let t of JSON_LOCS) {
if (t.city.includes('las')) {
test.push(t);
}
}
const test2 = JSON_LOCS.filter(loc => { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
loc.city.includes('las');
});
console.log(test); // Outputs a few results
console.log(test2); // Always empty! :(