loading...

re: Use this trick to map over single objects in Javascript VIEW POST

FULL DISCUSSION
 

Sorry to say this, but this "trick" is just a workaround for a poorly designed api. If you have to implement such kind of stuff, you should really reconsider reimplementing your api.

If an api endpoint should return a list, it has to return a list even if it only contains one element.

While such workarounds are possible in js, there is no trivial way in doing this in eg C#. And if the same api should get used for a .net project, the c# devs will get mad at you :-)

 

"Why aren’t objects iterable over properties, by default? The reasoning is as follows. There are two levels at which you can iterate in JavaScript:

The program level: iterating over properties means examining the structure of the program.
The data level: iterating over a data structure means examining the data managed by the program.
Making iteration over properties the default would mean mixing those levels, which would have two disadvantages:

You can’t iterate over the properties of data structures.
Once you iterate over the properties of an object, turning that object into a data structure would break your code. If engines were to implement iterability via a method Object.prototypeSymbol.iterator then there would be an additional caveat: Objects created via Object.create(null) wouldn’t be iterable, because Object.prototype is not in their prototype chain." - exploringjs.com/es6/ch_iteration.h...

 

I agree. In an ideal world, this would be true. But I'm talking more about the situation in which we're consuming APIs, not designing them. So the trick is not meant for the API devs but for the consumers who might run into issues because of unpredictable API results.

code of conduct - report abuse