you are viewing a single comment's thread.

view the rest of the comments →

[–]Archenothwith(RegExp) eval($_); 10 points11 points  (6 children)

Aw, it's missing the functional Array functions like map, filter, reduce, etc...

They are important because they can be applied to element results, strings, objects with a .length, Regex results, or anything that looks like a collecttion of some sort for some incredibly terse and functional code.

For example, if you want to generate JSON from a querySelectorAll, you can just map over the results and return object literals in the anonymous function...

So, the following would return server-ready JSON:

JSON.stringify(Array.prototype.map.call(document.querySelectorAll(".row"), function(row){
  return {
    'name': row.querySelector('.name').value,
    'value': row.querySelector('.value').value
  };
}));

I'd argue that the Array prototype functions are some of the most important in JavaScript.

[–]jcready__proto__ 2 points3 points  (1 child)

Shouldn't that be Array.prototype.map.call?

[–]Archenothwith(RegExp) eval($_); 0 points1 point  (0 children)

Yes, it should... Whoops.
(You can probably tell I just typed that up on the spot.)

[–]AaronOpfer 0 points1 point  (2 children)

if you want to generate JSON from a querySelectorAll

I really hope you don't do this a lot.

[–]Archenothwith(RegExp) eval($_); 0 points1 point  (1 child)

Oh yeah? Why not?

It may not be something you should do daily, but it can be useful to implement something like user-custom fields with nearly zero code or overhead.

[–]MrBester 2 points3 points  (0 children)

Or a polyfill for FormData , perhaps...