![]() |
filter() function question
I'm trying to understand the filter function and have run into a problem with my first test. :confused:
Why does the function seem to add 1 to each element of the original array but returns one less element in the new array? Code:
<!DOCTYPE HTML> |
Ready to kick yourself?
Doing return element++; means you return the ORIGINAL VALUE of the value of the element. That's what POST-INCREMENT means. "Get the value before the increment as the value of the expression, *then* do the increment." Because you are incrementing a LOCAL COPY of element, that effectively means you are just throwing away the incremented value; You will get *EXACTLY* the same results doing Code:
function addOne(element, index, array) Try this, instead: Code:
function addOne(element, index, array) { return (element++); }What were you *expecting* to happen?? |
function addOne(element, index, array) { return ++array[index]; }
var Aarr = [0,1,2,3,4,5,6,7,8,9]; Aarr.filter(addOne); alert(Aarr); |
Quote:
But you would get the same results using Code:
function addOne(element, index, array) If you were to do this: Code:
function addOne(element, index, array) { return ++array[index]; }But, again, if you started with (say) Code:
var Aarr = [0,1,-1,3,-1,7,0];Code:
Aarr == [ 1,2,0,4,0,8,1 ] |
So, again, I'm not at all sure what JMrker was trying to accomplish.
|
Quote:
And see that the new array had values all incremented by one. I thought I could avoid a for...loop with a filter() command. |
Well, it's kind of hacky, but you could do this:
Code:
function addOne(element, index, array) But, really, it would be more sensible to maybe do this: Code:
Array.prototype.addOne = function( ) |
Thanks. I like the prototype version the best.
Obviously I have a way to go to understand all the nuances of this JS language! |
Another easy option:
Code:
<script type="text/javascript">As demonstrated with the var Carr = line, if you omit the callback function then forEach turns into a simple array copy. (Not efficient, but better than crashing when the callback is omitted.) |
If you wanted, you could write that as
Code:
<script type="text/javascript"> |
Well, you've managed to confuse the fool out of me.
I started out trying to figure out the .filter() function, but have abandoned that because I thought I understood your Array.prototype function. Now I don't understand what's happening with your latest modification with the 'callback' addition. Here is the test code I am using to try to understand the working. In the comment section at the end, I list what I expected and what is displayed. ??? indicates that I don't understand the results. If you have the time, now that you have fogged my prior clarity, could you explain the difference between what I thought I know and what is really happening? (I may be testing your mind reading skills with that last statement). Code:
<script type="text/javascript"> |
Let's take them one at a time.
First of all, understand this: Whatever the callback function *RETURNS* for each element it is called for is what ends up in the OUTPUT array. Anything the function does to the *ORIGINAL* array has *NO IMPACT AT ALL* on what happens to the output array. SO; Code:
var Barr = Aarr.forEach( function(element) { return element + 1; } )Great. That's what you expected. That's what you got. ********* Code:
var BBarr = Aarr.forEach( function(element, array, index) { array[index] += 1; return element; } );Remember: the value of element *IS* the ORIGINAL value of the element. Changing the element value inside the current (thiis) array does *NOT* impact the value of element. I *THINK* that what you *MEANT* to do here was Code:
var BBarr = Aarr.forEach( function(element, array, index) { array[index] += 1; return array[index]; } );Code:
var BBarr = Aarr.forEach( function(element, array, index) { return ++array[index]; } );Code:
var Earr = Aarr.forEach( function(element, array, index) { array[index] *= 100; return element; } ); |
If you do NOT want to modify the input (this) array, then do *NOT* use the array or index arguments that are passed to the callback! Simple as that.
What you expected could have been produced via Code:
var Aarr = [0,1,2,3,4,5,6,7,8,9]; |
Maybe if we look at the forEach method it will be clearer. It truly is simple.
First of all, let me simplify it to *require* that the callback function be supplied: Code:
Array.prototype.forEach = function( callback ) {Code:
ar[i] = callback( this[i], this, i );Code:
callback( Code:
// using one of your example:Clearer? |
Thank you very much for that extensive discussion.
It helps me and perhaps others who may view this thread. I appreciate your time. |
| All times are GMT +1. The time now is 05:43 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.