Im trying to swap two array elements in an array that looks like this
[18785:Object, 22260:Object, 22261:Object, 22262:Object, 22263:Object]
I used the following code:
that.moveMediumDown = function(mediumID){
var arrKeys = new Array();
for (key in that.data.medium) {
arrKeys.push(parseInt(key));
}
for (var i = 0; i < arrKeys.length; i++){
if (arrKeys[i] === parseInt(mediumID)) {
//swap Medium
var tmpMedium = that.data.medium[arrKeys[i]];
that.data.medium[arrKeys[i]] = that.data.medium[arrKeys[i + 1]];
that.data.medium[arrKeys[i + 1]] = tmpMedium;
break;
}
}
//build new array with correct ids
var tmpMediumArray = new Array();
for (var j = 0; j < arrKeys.length; j++){
tmpMediumArray[arrKeys[j]] = that.data.medium[arrKeys[j]];
}
}
The problem is when I swap the content of the two array elements, the key stays the same. But I need also to swap the key.
So i tried to build a new array with the correct keys but then I get an Array with 22263 elements. Most of them are undefined and only the 5 are correct.
Is there any method to do this without getting such a big array?
Thanks in advance for your help.