Current location: Hot Scripts Forums » General Web Coding » JavaScript » move array element up or down


move array element up or down

Reply
  #1 (permalink)  
Old 09-25-03, 04:18 AM
Perry Perry is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Tennessee
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
move array element up or down

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
Reply With Quote
  #2 (permalink)  
Old 09-27-03, 10:13 AM
pat@barelyfitz.com pat@barelyfitz.com is offline
Newbie Coder
 
Join Date: Aug 2003
Location: Atlanta, GA, USA
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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.

Code:
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");

}
Reply With Quote
  #3 (permalink)  
Old 09-27-03, 02:52 PM
Perry Perry is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Tennessee
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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.spl ice("+elm+",1))");
}

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

}
</script>
Reply With Quote
  #4 (permalink)  
Old 09-27-03, 03:39 PM
pat@barelyfitz.com pat@barelyfitz.com is offline
Newbie Coder
 
Join Date: Aug 2003
Location: Atlanta, GA, USA
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Perry
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.
Reply With Quote
  #5 (permalink)  
Old 09-27-03, 04:23 PM
Perry Perry is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Tennessee
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional Array Sorting Kage PHP 6 12-12-05 04:19 PM
passing array to hidden fields in form on next page? seala ASP 2 09-04-03 02:40 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM
call, array and display? irfaan PHP 5 08-09-03 05:41 PM
Moving array index/values around Cagez PHP 1 07-15-03 02:34 AM


All times are GMT -5. The time now is 10:58 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.