PDA

View Full Version : Is there any way to select all items in an array without using a loop?



Snookerman
05-05-2010, 10:21 AM
I want to go through all checkboxes (all inputs are checkboxes) on a page and uncheck them if they are checked. This is what I came up with:

checkboxes = document.getElementsByTagName('input');
for (var index = 0; index < checkboxes.length; index++){
checkboxes[index].checked = false;
}
My first instinct was to just do one of these:

checkboxes = document.getElementsByTagName('input');
checkboxes.checked = false;
//or
checkboxes[all].checked = false;
That of course doesn't work, but is there any other way to do something to all items in an array without using loops? Thanks!

djr33
05-05-2010, 05:45 PM
My instincts tell me no. The "way" would involve still looping through each whether or not you look at it. You could write a function called "dotoarray()" something like array_map() in PHP, but I'm not sure how that would be helpful.
There may be some trick, though.

Snookerman
05-05-2010, 08:33 PM
After some looking around, I found two methods, forEach and map, that look like they might help here, but I've no idea how. I still have a lot to learn.

djr33
05-05-2010, 08:54 PM
Isn't foreach like in PHP, then? And I expect map() is like array_map() in PHP. The effect with both though is that you are actually using a loop-- it's just not part of the code that you write. For processing speed, etc., I expect it would be the same.