Working with arrays passed byref

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-04-23T02:20:30Z Indexed on 2010/04/23 2:23 UTC
Read the original article Hit count: 316

Filed under:
|
|
|

I would like for someone to explain this to me:

function myFunction(array){
    array = $.grep(array, function(n,i){return n > 1 });
}

var mainArray = [1,2,3];

myFunction(mainArray);
document.write(mainArray) // 1,2,3, but i'm expecting 2,3

but if i do something like

    array[3] = 4;

in place of the $.grep line, i get 1,2,3,4. Shouldn't mainArray become the new array created by $.grep?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery