views:

89

answers:

5

Hi,

in JQuery i m having an array like

(1,2,6,8)

I have already selected the first element that is 1 which i have saved in a JQuery variable

submitterid = 1

On clicking a link I am trying to get the next greatest element in the Array than what I have selected in the submitterid..

How can I achieve this?

Edit:

How to find the last element in this array in the code

  var previousId;

               $("#previous").click(function (){


   index = submitters.indexOf(submitterid),
       nextId;
   if (index - 1 < submitters.length) {
       previousId = submitters[index-1];
   } else {
       // no ID index

        // if i am having an array of 1,2,6,8 after moving to 1 from 8 to 6 - 2-1 i am trying to move to the last element of the array
   }

   alert(previousId);




     });// previousId
+4  A: 

Why couldn't you do something like:

var arr = [3, 5, 8, 3].sort(function (a, b) { return a - b; } );
var val = arr.pop();

Any keep popping the array -- saying that the values don't need to stay in the array.

If you are randomly picking values and you need the next highest, then write the appropriate sorting function.

Justin Van Horne
You don’t need to specify a comparison function since that’s the default comparison function.
Gumbo
it was to demonstrate :)
Justin Van Horne
A: 

You have to loop through the array. Try this untested code:

// Your test array.
var arrValues = [ "1", "2", "6", "8" ];

var low = 1;
var high = 2;

// Loop over each value in the array.
$.each( arrValues, function( intIndex, objValue ){

if (objValue > high)
  hight = objValue;

if (objValue < high)
  if (objValue > low)
    low = objValue

});

return low;
Steven
Justin's code was also a good idea. Sort the array, with highest in the end. Find position of last element, and return last element -1
Steven
A: 

Actually, there is no need to use any jquery-specific stuff for this, apart from the click event handling, just 'plain javascript' will do;

var myArray = [ 1, 2, 6, 8 ];
var submitterid = 1;

$(function() {
  $('a#id').click(function(event) {
    event.preventDefault();
    var greater = -1;
    // loop through array
    for (var i in myArray)
      // find numbers greater than the specified number
      if (i > submitterid)
        // find numbers closest to specified number
        if (i < greater || greater < 0)
          greater = i;

    if (greater < 0) {
      // no greater value found, do something
    } else {
      // next biggest value is in variable greater, do something with it
    }
  });

});
kkyy
+1  A: 

You want a counter:

function counter(arr) {
  this.arr = arr;
  this.index = 0;
}

counter.prototype.next = function() {
  return this.arr[this.index++];
}

You instantiate it and use it like:

var nums = new counter([1,2,3,4,5]);
nums.next() ; => 1
nums.next() ; => 2
nums.next() ; => 3
Jeff Ober
Wouldn't this assume that the numbers are already sorted low->high? The numbers may be 1,6,3,9,7.
Steven
I don't think the asker wanted to know how to sort; I think he meant they wanted the next largest index. His array is already sorted in the example.
Jeff Ober
A: 

If your array is already sorted (see sort method), try this:

var arr = [1,2,6,8],
    submitterid = 1,
    index = arr.indexOf(submitterid),
    nextId;
if (index + 1 < arr.length) {
    nextId = arr[index+1];
} else {
    // no ID index
}
Gumbo
how to do the same for previous see my Edit ..
Aruna
@Aruna: Replace `index + 1 < arr.length` by `index - 1 >= 0` and `nextId = arr[index+1]` with `prevId = arr[index-1]`.
Gumbo