Or a simple reduce for the functional way
numbers.reduce((acc,val)=>{ return (val%2===1 ? {...acc,odd:[...acc.odd,val]}:{...acc,even:[...acc.even,val]}); },{odd:[],even:[]})
Yeah, I would've gone with the reduce too, since that's only one iteration over the array, not two. Not sure I'd use spread though, wouldn't that kill the gain?
Probably it would, so just mutate the acc and return it, I think even after all the solutions, I think the simple for loop performs the best.
Hey there,
thanks for your solution! Nice opportunity for people who want to step up their reduce-skills.
reduce
I was just going to comment, that this would also be done with reduce (my favorite)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Or a simple reduce for the functional way
numbers.reduce((acc,val)=>{
return (val%2===1 ? {...acc,odd:[...acc.odd,val]}:{...acc,even:[...acc.even,val]});
},{odd:[],even:[]})
Yeah, I would've gone with the reduce too, since that's only one iteration over the array, not two. Not sure I'd use spread though, wouldn't that kill the gain?
Probably it would, so just mutate the acc and return it, I think even after all the solutions, I think the simple for loop performs the best.
Hey there,
thanks for your solution!
Nice opportunity for people who want to step up their
reduce
-skills.I was just going to comment, that this would also be done with reduce (my favorite)