1

Getting something very odd happening with an array in my code. I have made a JS fiddle to demonstrate (you will have to open up your browsers console to see the logs).

I have also provided an image high lighting whats happening.

I would like some explanation as to why this might be happening.

Basically, an array of size 3 only shows 2 of the elements within the array. When i console log the array.length, it shows as 3, however upon opening the object only 2 elements are visible, something doesn't seem right here to me.

EDIT: I used the console log simply to show what was happening. My real issue is I cant access this 3rd element in the code, and I need to.

Please see the JS fiddle & image attached.

https://jsfiddle.net/ysf2o39f/

console.log('Init');
var arr = []; // Array of objects
var obj = {}; // Dummy object
// Fill array
arr.push(obj);
arr.push(obj);
arr.push(obj);
// Log
console.log('Before pop: ');
console.log(arr);
console.log('Length: ' + arr.length);
// Pop array
arr.pop();
// Log
console.log('After pop: ');
console.log(arr);
console.log('Length: ' + arr.length);

Image of the issue

aayala
  • 29
  • 3
  • Try executing this one by one individually in the console and the issue will likely disappear. – deceze Dec 16 '15 at 11:17
  • I used the console log simply to show what was happening. My real issue is I cant access this 3rd element in the code, and I need to. – aayala Dec 16 '15 at 11:19
  • 2
    Try to log a _copy_ of the array into the console using `arr.slice()` and you will see the behaviour is now corrected. This has to do with how the console stores and displays data, in this case both are displaying the modified array. – somethinghere Dec 16 '15 at 11:20
  • Yes, when you access in the console to the details of the array, it is modified and it shows the modified array, as @somethinghere said – Marcos Pérez Gude Dec 16 '15 at 11:23
  • 1
    You will not have problems in your development if you try to access third element. Your code is not selecting in the console. – Marcos Pérez Gude Dec 16 '15 at 11:24
  • Your *only* issue in this sample is the console. Programmatic access should work just fine. If it doesn't, provide an actual representative sample. – deceze Dec 16 '15 at 11:25
  • I can't provide the actual example unfortunately. Its part of quite a large application with various classes. – aayala Dec 16 '15 at 11:27
  • But you have no problem accessing the third element in the provided example, right? In that case this is all we can do for you. – somethinghere Dec 16 '15 at 11:27
  • Then you'll have to debug your large application and possibly reduce it to a representative sample. We can't tell you what's wrong with your application with the given code, since there is nothing wrong with the code given. – deceze Dec 16 '15 at 11:28
  • OK. That is understandable, I'll make work on reducing it. – aayala Dec 16 '15 at 11:34

0 Answers0