2

I did look out for some other answers on SO in regards, and from one of them I found the one I am using on here, yet it looks like it's never happening as this isn't firing return false; neither the console.log(myId); which normally should.

      let pattern = new RegExp('^\/wiki\/');
      var total = doSelect("Location").siblings('td').find('a').length;
      doSelect("Location").siblings('td').find('a').each(function(i, el, index) { 
        var result = $(this).attr('href').replace(pattern, '');
        console.log(result);
        if (index === total - 1) {
          myId = item.pageid;
          console.log(myId);
          return false;
        }
      });

I even tried the following as suggested on this other SO answer with no positive results:

  let pattern = new RegExp('^\/wiki\/');
  doSelect("Location").siblings('td').find('a').each(function(i, el) { 
    var result = $(this).attr('href').replace(pattern, '');
    console.log(result);
  }).promise().done( function(){ 
    myId = item.pageid;
    console.log(myId);
    return false;
  });

UPDATE

About: item (no issue with the following data), the above code is within the following data each:

  $.getJSON(url,function(data){
    $.each(data, function(i, item) {
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • what is `item`? – er-han Jul 29 '17 at 22:31
  • 1
    @er-han updated with the answer to your question in case it is needed to others – rob.m Jul 29 '17 at 22:32
  • 1
    Shouldn't you simply use `i` instead of `index` in your if statement? – Mehdi Jul 29 '17 at 22:34
  • @MehdiElFadil I could, but is it that you think that causes the problem? – rob.m Jul 29 '17 at 22:35
  • Why do you need to be aware of when the last index is reached? What are you trying to achieve? `.promise()` will resolve a jQuery deferred object immediately if not chained to a function which returns a jQuery deferred object or a custom queue name – guest271314 Jul 29 '17 at 22:35
  • I think @Mehdi_El_Fadil is right – er-han Jul 29 '17 at 22:37
  • @MehdiElFadil it was indeed that, would you place it on as an answer and I will accept it? Other people have answered the same this and I'd want to accept the help – rob.m Jul 29 '17 at 22:38
  • Also you'd better change `i` to something else because the upper `each` function has an `i` named argument too – er-han Jul 29 '17 at 22:40
  • 1
    @er-han yup, going to accept MTK answer as it kinda mentions that – rob.m Jul 29 '17 at 22:40
  • If you just need the last anchor, using `last()` would be a much better option than iterating over all of them with `each()` – adeneo Jul 29 '17 at 22:42
  • @er-han not really relevant as it is a closure – charlietfl Jul 29 '17 at 22:42
  • 1
    @charlietfl what if the upper iterating index is needed too? Maybe not now, but it is good for more readable code – er-han Jul 29 '17 at 22:44

4 Answers4

2

There is no third argument for each() callback. What you refer to as index is undefined. The first argument i is the index

Change to:

if (i === total - 1)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
2

Your first option have one sintax error:

.each(function(i, el, index) must be .each(function(index, el)

 let pattern = new RegExp('^\/wiki\/');
  var total = doSelect("Location").siblings('td').find('a').length;
  doSelect("Location").siblings('td').find('a').each(function(index, el) { 
    var result = $(this).attr('href').replace(pattern, '');
    console.log(result);
    if (index === total - 1) {
      myId = item.pageid;
      console.log(myId);
      return false;
    }
  });
MTK
  • 3,300
  • 2
  • 33
  • 49
1

The problem is that the variable index is not defined, so if (index === total - 1) never returns true.

The call to function .each:

.each(function(i, el, index)

should be

.each(function(i, el)

Then the test should be

if (i === total - 1)

cf jQuery API documentation, stating the two arguments taken by the function:

.each( function )

function Type: Function( Integer index, Element element )

Mehdi
  • 7,204
  • 1
  • 32
  • 44
0

You can also use .last() function:

1.

//insert own jq function
$.fn.myTask = function(){
  myId = item.pageid;
  console.log(myId);
  //My suggestion: return this;
  return false;
}

2.

  //than replace your lines
  let pattern = new RegExp('^\/wiki\/');
  var total = doSelect("Location").siblings('td').find('a').length;
  doSelect("Location").siblings('td').find('a').each(function(i, el) { 
    var result = $(this).attr('href').replace(pattern, '');
    console.log(result);
  }).last().myTask();
Tomasz Durda
  • 134
  • 1
  • 9