Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm wondering for some time now why this fails:

if (longest.length >= 3) {
    for ( var i = 0; i < longest.length-1; i++) {
      var $last[i] = longest[i].splice(-1).toString();
      //if ( $( $last[i] ).closest(':jqmData(role="panel")') == "popover" ) { 
      //var gotoPage = $last[i];
    // }
      }
    }

longest is an array, which contains array elements.

I want to loop through the number of arrays in longest and create variables from each nested arrays' last element. The .splice(-1).toString() fails telling me "missing ; before statement"

What am I doing wrong?

EDIT: longest will look something like this:

[[#menu, #menuSub1, #menuSub2], [#main, yield, yield], [#pop1, #pop1-1, #pop1-2]]

It's within a function to track the browser-history in a mobile-app with different panels.

EDIT2: Finished code (Thx Mic):

if (longest.length >= 3) {
   var $last = [];
   for ( var i = 0; i < longest.length; i++) {
      $last.push( longest[i][ longest[i].length - 1 ]);
      if ( $( $last[i] ).closest(':jqmData(role="panel")').jqmData('panel')  == "popover" ) { 
         var gotoPage = $last[i]; }
      }
   }
share|improve this question
 
what is $last?? –  Baz1nga Oct 21 '11 at 19:18
 
should be the variable name. $last0, $last1, $last2... –  frequent Oct 21 '11 at 19:18
 
I believe splice() requires two parameters, both being positive integers or zero. –  Diodeus Oct 21 '11 at 19:21
 
You have one extra "}" out there, you probably forgot to comment out. –  Birey Oct 21 '11 at 19:21
 
@Diodeus: I'm using it quite a lot, always like this, so I think that won't help. I think either chainging splice().toString() doesn't go or I can't declare variables like this: $last[i] –  frequent Oct 21 '11 at 19:24
add comment

1 Answer

up vote 5 down vote accepted

You should write something like:

if (longest.length >= 3) {
    var $last = [];
    for ( var i = 0; i < longest.length-1; i++) {
      $last.push( longest[i][ longest[i].length - 1 ] );
    }
  }

and get the values with $last[0]

Or use a hash like:

if (longest.length >= 3) {
    var hash = {};
    for ( var i = 0; i < longest.length-1; i++) {
      hash['$last'+ i] = longest[i][ longest[i].length - 1 ];
    }
  }

and get the content using for instance hash.$last0

share|improve this answer
 
may be give a sample of longest, it would help –  Mic Oct 21 '11 at 19:27
 
edited my code above –  frequent Oct 21 '11 at 19:31
 
I edited the answer, is it better? This is ok if you have just 2 levels of arrays, if you have more, it needs to recurse –  Mic Oct 21 '11 at 19:31
 
longest.length-1 is a bit strange as you will never reach the last element of longest –  Mic Oct 21 '11 at 19:33
 
Your first snippet works perfectly! THANKS. I'll probably be dreaming "array-style" tonight... –  frequent Oct 21 '11 at 19:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.