Array clone #1120

hadrienk opened this Issue Apr 12, 2012 · 1 comment


None yet
3 participants

When using an array with non-continuous indexes, the clone method behaves incorrectly.

var test = new Array();
test[10] = 1;
test[5] = 1;
var clone = test.clone();

The previous code will return an array with undefined values from 0 to 4 and from 6 to 9

The problem comes from the function

Array.implement('clone', function(){
    var i = this.length, clone = new Array(i);
    while (i--) clone[i] = cloneOf(this[i]);
    return clone;

boppy commented Apr 22, 2012

I think it's more an ECTA/Java-Script thing and how they internaly treat arrays ECMA doc wants it to be like this (see, page 126). Try that even without mootools present

var test = new Array();
test[10] = 1;
test[5] = 1;

So the logic behind that non-associative array is that there will not be a NULLed (undefined) element in it. You should therefore use objects to store an assoc array, I think...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment