javascript - Swapping Array elements with Keys -
im trying swap 2 array elements in array looks this
[18785:object, 22260:object, 22261:object, 22262:object, 22263:object]
i used following code:
that.movemediumdown = function(mediumid){ var arrkeys = new array(); (key in that.data.medium) { arrkeys.push(parseint(key)); } (var = 0; < 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 correct ids var tmpmediumarray = new array(); (var j = 0; j < arrkeys.length; j++){ tmpmediumarray[arrkeys[j]] = that.data.medium[arrkeys[j]]; } }
the problem when swap content of 2 array elements, key stays same. need swap key.
so tried build new array correct keys array 22263 elements. of them undefined , 5 correct.
is there method without getting such big array?
thanks in advance help.
checkout array_flip function.
it allows swap keys elements:
array_flip( {a: 1, b: 1, c: 2} );
becomes
{1: 'b', 2: 'c'}
Comments
Post a Comment