Skip to content

Instantly share code, notes, and snippets.

@pocotan001
Last active June 1, 2018 09:33
Show Gist options
  • Save pocotan001/981d558660c147af3fa5391bc32212a5 to your computer and use it in GitHub Desktop.
Save pocotan001/981d558660c147af3fa5391bc32212a5 to your computer and use it in GitHub Desktop.
Create an object instead of an array
const ABC = ["a", "b", "c"];
ABC.reduce((acc, key) => {
acc[key] = `${key}!`;
return acc;
}, Object.create(null));
ABC.reduce(
(acc, key) => Object.assign(acc, { [key]: `${key}!` }),
Object.create(null)
);
ABC.reduce(
(acc, key) => ({
...acc,
[key]: `${key}!`
}),
Object.create(null)
);
// { a: "a!", b: "b!", c: "c!" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment