Reputation: 1350
I have extracted an array of objects, from the following raw data: https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json
Suffice to say, the data looks something like this:
[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", …}
I've stored this as const JSON_LOCS
, referenced in the code below.
I'm trying to filter this down looking for cities that include some specific test. I've approached it 2 different ways. One way seems to work, but Array.prototype.filter()
doesn't.
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); // Yields a couple of results
console.log(test2); // Always empty! :(
Upvotes: 0
Views: 44
Reputation: 5136
remove the { }
const test2 = JSON_LOCS.filter(loc => { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
loc.city.includes('las');
});
into
const test2 = JSON_LOCS.filter(loc => // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
loc.city.includes('las'); // When not wrapped into {} it assumes its the return statement
);
Upvotes: 1
Reputation: 4388
Instead of this line
oc.city.includes('las');
write this line
return oc.city.includes('las');
you simply forget the return statement which in this case will return undefined
Upvotes: 1