Skip to content

Instantly share code, notes, and snippets.

@bryzettler
Last active June 1, 2017 04:13
Show Gist options
  • Save bryzettler/f8a97f6845513b5b9e83430d9bced757 to your computer and use it in GitHub Desktop.
Save bryzettler/f8a97f6845513b5b9e83430d9bced757 to your computer and use it in GitHub Desktop.
const records = [
{ title: "The Clash", artist: "The Clash", type: "LP", lengthSec: 2220, released: "1977"},
{ title: "Rocket to Russia", artist: "Ramones", type: "LP", lengthSec: 1906, released: "1977"},
...etc
];
// es5
const way = records.reduce((acc, record) => {
acc[`${record.title} - ${record.artist}`] = record;
return acc;
}, {});
// es6
const way = records.reduce((acc, record) => ({
...acc,
[`${record.title} - ${record.artist}`]: record,
}), {});
/*
=> {
"The Clash - The Clash": { artist: "The Clash", lengthSec: 2220, released: "1977", title: "The Clash", type: "LP"},
"Rocket to Russia - Ramones": { artist: "Ramones", lengthSec: 1906, realease: "1977", title: "Rocket to Russia", type: "LP"},
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment