View Single Post
Old 12-15-2012, 12:46 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Do you think it relevant enough to put into the 'Post A Javascript' section?
Up to you, although Logic Ali may have trumped you
Code:
    var arr = [];
    for (var i = group.length; i--; arr.unshift(group[i]));
    return arr.map(function (obj) {
        return (obj.checked) ? obj.value : false;
    }).filter(function (obj) {
        return (obj !== false);
    });
I'm not suggesting this should be incorporated - there is no added value, I was just experimenting .

A DOM nodeList is like an array but is not an array - it's really a collection of objects. This code first stores (converts?) the nodeList to an array.

The array map method creates another array by applying a function to every element of an array. In this instance, the new array contains either the value false, or the value of the input; that is, if it is checked.

The method filter is then applied to the resultant array. This removes the false elements from the array, leaving only the values of those elements which were checked.

It is not really useful for this example. It could be useful if a lot more processing is done within the map or filter methods. Andy.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
jmrker (12-15-2012)