Created
March 10, 2020 13:24
-
-
Save munkacsitomi/6a0d08742263b308d2ec56b63d0b2a6b to your computer and use it in GitHub Desktop.
We don't need to use reduce if it's not necessary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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