PDA

View Full Version : move array element up or down


Perry
09-25-03, 05:18 AM
suppose I have an array like
myArray = Array("item one","item two","item three","item four")

I want to call a function with three parameters, one being the array the second being the element to move, and finally the direction to shift

example: onClick="moveElement(myArray,'item two','down')"

myArray above would become:
("item one","item three","item two","item four")

I've seen this functionality for <select> boxes but I don't think that's adaptable here.

~Perry

pat@barelyfitz.com
09-27-03, 11:13 AM
Thanks for the help on my other question (not sure if that will solve the problem but I'll check it out).

Here's a possible solution to your problem. Note that it adds a method to the Array object, so you can use it with any array (assuming it works). Also note that it only switches two elements - so if you use a delta of -3, for example, it will switch the two items but will not "bump up" the in between items - that is left as an exercise for the reader. :)

Array.prototype.move_element = function(index, delta) {

// This method moves an element within the array
// index = the array item you want to move
// delta = the direction and number of spaces to move the item.
//
// For example:
// move_element(myarray, 5, -1); // move up one space
// move_element(myarray, 2, 1); // move down one space
//
// Returns true for success, false for error.

var index2, temp_item;

// Make sure the index is within the array bounds
if (index < 0 || index >= this.length) {
return false;
}

// Make sure the target index is within the array bounds
index2 = index + delta;
if (index2 < 0 || index2 >= this.length || index2 == index) {
return false;
}

// Move the elements in the array
temp_item = this[index2];
this[index2] = this[index];
this[index] = temp_item;

return true;
}

//--------------------------------------------------
// Test it out

myArray = Array("item one","item two","item three","item four")

if ( myArray.move_element(3, -1) ) {

// Success, display the array contents
alert("myArray = " + myArray);

} else {

// Failure
alert("move_element returned false");

}

Perry
09-27-03, 03:52 PM
Before I saw your post, I came up with this function. Comments/Suggestions?

<script>
var myArray = Array("item zero","item one","item two","item three","item four");

function moveIt(arrayName,elm,direction){

if(direction == "up" && elm != 0){
eval("arrayName.splice("+(elm-1)+",0,arrayName.splice("+elm+",1))");

}else if(direction == "down" && elm != arrayName.length){
eval("arrayName.splice("+(elm+1)+",0,arrayName.splice("+elm+",1))");
}

//debug output
document.getElementById('debug').innerHTML=printAr ray(arrayName);

}
</script>

pat@barelyfitz.com
09-27-03, 04:39 PM
Before I saw your post, I came up with this function. Comments/Suggestions?

Seems needlessly complex with the evals and splices - imagine coming back in a month and trying to figure out what the code does.

Perry
09-27-03, 05:23 PM
I've never used Array.prototype - I'm not as comfortable with javascript as I am with PHP. Scarred for life by incompatible DOMs I suppose.

I tried your method and it works perfectly; I think it'll be more flexible also.

Thanks and if you need any more help with Mac compatiblity,just drop a line:

perry at perryjohnson dot info