0

I am doing the following:

  $.getJSON(url,function(data){
    var totalQueries = data.length;
    $.each(data, function(i, item) {

But this one looks like to be wrong:

var totalQueries = data.length;

As by the end of it I check for the last item and it never happens:

  if (i === totalQueries - 1) {
    myId = item.pageid;
    console.log(myId);
    newQuery();
  }
19
  • 1
    Are you getting console errors? Have you tried debugging? How confident are you that data is an array and not an object? Commented Jul 30, 2017 at 3:02
  • I am sure data is an array as I can read it, I get no errors as the code is correct, but I can see that console.log(myId); doesn't happen which means the conditional isn't happening @AlexanderNied Commented Jul 30, 2017 at 3:03
  • 1
    total length of a function(data) makes no sense ... you're getting the length property of the returned parsed JSON. If the top level "object" is an Array, the length property is the number of elements in the array Commented Jul 30, 2017 at 3:06
  • perhaps an example of data would help - perhaps the last item in the array is not an object with a pageid property Commented Jul 30, 2017 at 3:07
  • @JaromandaX exactly, I knew it wasn't making any sense, that's why I have ended up on here. Is there anyway I could do to know the end of it? Commented Jul 30, 2017 at 3:07

1 Answer 1

0

data is object use Object.keys(data).length; to count and here you object look like,

i is parse

{
  "parse": {
    "title": "Colegio Nueva Granada",
    "pageid": 2340380,
    "text": {
      "*": "<div class=...."
    },
    "langlinks": []
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried assigning var totalQueries = Object.keys(data).length; but I never get the console.log(myId);
because i is object key parse not interger, you need to create counter.
can you edit my fiddle so that i better understand your answer please? Because I am not sure to put the counter
here jsfiddle.net/ewwink/72uL633e edited: #65, $199, added: #66, #197, #198, 204# beware it load huge file, disable your internet before it hang your browser
hey thanks, I am not sure why it is loading so many if the continue parameter is 2 and i can see a lot of duplicate ids tho
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.