Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created June 13, 2017 18:09
Show Gist options
  • Save wesbos/aa9b392835fd12af37828a4b5891f4a6 to your computer and use it in GitHub Desktop.
Save wesbos/aa9b392835fd12af37828a4b5891f4a6 to your computer and use it in GitHub Desktop.
const countryCodes = {
US: 'United States',
CA: 'Canada',
NG: 'Nigeria',
GB: 'United Kingdom',
};
const sales = [
{ code: 'US', count: 233 },
{ code: 'CA', count: 200 },
{ code: 'GB', count: 150 },
{ code: 'NG', count: 111 },
];
const salesMix = sales.map(sale => {
return {
...sale, // object spread takes does a shallow copy of the sale object
country: countryCodes[sale.code]
};
});
// or the hotshot way with implicit return:
const salesMixHotShot = sales.map(sale => ({...sale, country: countryCodes[sale.code] }));
console.table(salesMix);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment