Skip to content

Instantly share code, notes, and snippets.

@jjsub
Forked from wesbos/object-spread.js
Created June 16, 2017 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjsub/2188f54bb9f933a5162bcf1401746d27 to your computer and use it in GitHub Desktop.
Save jjsub/2188f54bb9f933a5162bcf1401746d27 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