Skip to content

Instantly share code, notes, and snippets.

@bunnyhawk
Created November 7, 2020 16:06
Show Gist options
  • Save bunnyhawk/3439ed2c0d719c0cc13ebc5e79924488 to your computer and use it in GitHub Desktop.
Save bunnyhawk/3439ed2c0d719c0cc13ebc5e79924488 to your computer and use it in GitHub Desktop.
const counties = [
{ name: 'Clackamas', state: 'Oregon', population: 380000 },
{ name: 'King', state: 'Washington', population: 2000000 },
{ name: 'Kitsap', state: 'Washington', population: 250000 },
{ name: 'Multinomah', state: 'Oregon', population: 750000 },
{ name: 'Snohomish', state: 'Washington', population: 720000 },
{ name: 'Wasco', state: 'Oregon', population: 25000 },
];
const highPopulousCounties = counties.reduce((acc, { name, state, population }) => {
// This filters out anything smaller than 700000
if (population <= 700000) return acc;
// Again using a spread here. If the array is already started,
// it will fill, otherwise it'll initialize a new array.
const updatedAcc = [...acc];
updatedAcc.push({ fullName: `${name} County, ${state}`, population });
return updatedAcc;
}, []);
// Returns:
// [
// { fullName: 'King County, Washington', population: 2000000 },
// { fullName: 'Multinomah County, Oregon', population: 750000 },
// { fullName: 'Snohomish County, Oregon', population: 720000 },
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment