Skip to content

Instantly share code, notes, and snippets.

@godmar
Created June 18, 2020 00:36
Show Gist options
  • Save godmar/f7012d3f05c86a3c8ad76e35afa9bbf7 to your computer and use it in GitHub Desktop.
Save godmar/f7012d3f05c86a3c8ad76e35afa9bbf7 to your computer and use it in GitHub Desktop.
output is:
countries = [
{ country: "US", rank: 3 },
{ country: "US", rank: 4 },
{ country: "UK", rank: 3 },
{ country: "UK", rank: 3 },
{ country: "Germany", rank: 7 },
];
let o1 = Object.fromEntries(countries.map(c => [c.country, 1]));
console.log(o1);
let a1 = Object.keys(o1);
console.log(a1);
let o2 = a1.map(country => ({ country: country })); // the () around { } are important
console.log(o2);
@godmar
Copy link
Author

godmar commented Jun 18, 2020

output is

{ US: 1, UK: 1, Germany: 1 }
[ 'US', 'UK', 'Germany' ]
[ { country: 'US' }, { country: 'UK' }, { country: 'Germany' } ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment