Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Created March 10, 2020 13:24
Show Gist options
  • Save munkacsitomi/6a0d08742263b308d2ec56b63d0b2a6b to your computer and use it in GitHub Desktop.
Save munkacsitomi/6a0d08742263b308d2ec56b63d0b2a6b to your computer and use it in GitHub Desktop.
We don't need to use reduce if it's not necessary
const ids = [100, 101, 102, 103];
// We can use reduce to wrap ids in an object
const first = ids.reduce((out, id) => {
out.push({ id });
return out;
}, []);
// Or use map to do the same
const second = ids.map((id) => {
return { id: id };
});
// And it could be a one-liner
const third = ids.map((id) => ({ id }));
console.log(first);
console.log(second);
console.log(third);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment