DEV Community

Discussion on: Use this trick to map over single objects in Javascript

Collapse
 
rehmatfalcon profile image
Kushal Niroula

Wouldn't it be better to just wrap the code to be executed for each item in a method and then call the method directly if it is single item?

Say, something like

if(!Array.isArray(example)) return doSomething(example);
return example.map(doSomething);