1

I have the following test code:

console.log('AA',slider);
console.log('AB',slider.length);

it returns the following in chrome console.

AA Array[53]
AB 0

i added test code because slider[5] always came back with undefined even though the console shows there is a value there.

here is a simplified version of initialisation script. entire code is pretty long and Slider is an object. Code for object is working the test script is later on trying to manipulate specific slider positions based on ajax return data.

var slider=[];
for (var uid=1;uid<50;uid++) {
    slider[uid]=new Slider(.........);
}

var slider={};
for (var uid=1;uid<50;uid++) {
    slider[uid]=new Slider(.........);
}

returns AA { 1:{m a: ...., b: ...., c: ...., g: ...., h: .... }, 2: .....

gets all the way up to 49

  • 4
    Could you please post the code that initializes `slider`? Thanks – Christos Feb 05 '17 at 06:52
  • `based on ajax return data` - just as I thought ... asynchronous code ... change your `console.log('AA',slider);` to `console.log('AA', slider.slice());` - and you'll see it's empty ... your problem is most likely knowing how to use results from asynchronous call - see https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Jaromanda X Feb 05 '17 at 06:59
  • 1
    Cannot reproduce https://jsfiddle.net/b3pgam6d/. – guest271314 Feb 05 '17 at 06:59
  • Why does your array index `uid` start at `1` instead of `0`? In javascript array indices start at `0`. – Darin Dimitrov Feb 05 '17 at 06:59
  • @Darin Dimitrov uid can have any value and does not need to be sequential which is why in simplified I started at 1. It is also why I changed to object just now since if first uid is 10,000,000 it would waste a lot of ram. – Matthew Cornelisse Feb 05 '17 at 07:02
  • 1
    okay, but then if you try to access `slider[0]` you will get `undefined`. I thought that might somehow be related to your problem here. – Darin Dimitrov Feb 05 '17 at 07:02
  • 1
    @MatthewCornelisse See http://stackoverflow.com/mcve – guest271314 Feb 05 '17 at 07:06
  • @guest271314 i get 404 error – Matthew Cornelisse Feb 05 '17 at 07:07
  • 1
    http://stackoverflow.com/help/mcve – Darin Dimitrov Feb 05 '17 at 07:07
  • @MatthewCornelisse _"i get 404 error"_ How is that related to original Question _"How is it possible to have an array with length 0 but has content?"_ ? – guest271314 Feb 05 '17 at 07:08
  • @Christos pretty sure it is not an asynchronous error because it is getting the problem at startup while reading the last known state from local storage. – Matthew Cornelisse Feb 05 '17 at 07:11
  • 1
    @guest271314 He's referring to the link you posted. – evan.stoddard Feb 05 '17 at 07:12
  • @guest271314 i am trying to give useful examples but I am not entirely sure how it got in that state in the first place. Will have to take it all apart tomorow after I get some sleep and see if I can come up with a complete example of how the problem came to exist. – Matthew Cornelisse Feb 05 '17 at 07:13
  • @evan.stoddard Ok. DarinDimitrov posted correct link. – guest271314 Feb 05 '17 at 07:14
  • @MatthewCornelisse Ok. Cannot reproduce `.length` of `0` where array has element, save for `var arr = []; arr["prop"] = 53; console.log(arr.length)`. Not clear what issue is? See also http://stackoverflow.com/help/how-to-ask – guest271314 Feb 05 '17 at 07:15
  • 1
    @MatthewCornelisse Going out on a limb here. If you're initializing `var slider` with `var slider = {}` then it's not an array, it's an object, not an array. So trying to index it and populate it that way will result in an undefined value for `slider.length`. This is based on your second snippet of code. https://jsfiddle.net/wc1govmt/ – evan.stoddard Feb 05 '17 at 07:19
  • @evan.stoddard sorry should have specified I changed second line of test code to `console.log('AB',Object.keys(slider).length);` – Matthew Cornelisse Feb 06 '17 at 14:39
  • I have not yet been able to reproduce on simplified code. On full code it does every time. What I find most strange is console on chrome clearly shows on first test line there is an object full of type Sliders(which closure compiler changes to m) and line 2 of test code shows there is nothing in the object. – Matthew Cornelisse Feb 06 '17 at 14:41

1 Answers1

0

Ok i figured out the problem and hopefully this helps others.

After going through the code here is a much better example of initialization

var slider=[];
setInterval(function(){
    for (var uid=1;uid<50;uid++) {
        slider[uid]=new Slider(.........);
    }
},500);
console.log('AA',slider);
console.log('AB',slider.length);

the array is indeed empty at the time the console log commands are executed but apparently the first line records reference to the object and not current state. So when I look at the log which is always going to be more then half a second later it shows data being there.

  • While this probably works, it seems like you're not going about this the right way. You are getting data with ajax, which is asynchronous. You should look into using callbacks to deal with that or promises. http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ https://davidwalsh.name/promises – evan.stoddard Feb 07 '17 at 21:20
  • I am using callbacks to handle the data. The problem was while trying to troubleshoot a problem unrelated to Ajax call. Since the console log shows current value of object instead of value at the time of command execution it resulted in me seeing an unexpected value. The objects value was being modified after I was trying to see what it's value was – Matthew Cornelisse Feb 08 '17 at 22:58