Chrome developer tool Array length [duplicate] - javascript

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 5 years ago.
Why chrome developer tool showing 0 array length (first line Array[0]) even if there are total 9 objects in it?
At first line of image should be like Array[9] why it is showing Array[0]
In second image array has 13 objects so it is showing Array[13] not Array[0]

It seems like you are logging the output of the array before you are adding objects to your array. Something like
var arr = [];
console.log(arr);
arr.push({}); //pushing blank obj
Here, executing this code will result in Array[0] but it does hold your object and will return a length of 1 if you try arr.length.
This might also happen if you are having some sort of Async function which pushes item to your array, which will result in the same thing. For example,
var a = [];
setTimeout(function(){
a.push({});
a.push({});
}, 1000);
console.log(a);
I think, this behavior is intentional, where Chrome wants to show that initially, your Array was empty, later on, items were pushed on runtime.

Related

Same random number twice [duplicate]

This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 3 years ago.
Why does this show the same random number twice when you run it in your console?
var arr = [[]];
arr[0][0] = Math.random();
console.log(arr);
arr[0][0] = Math.random();
console.log(arr);
I derived this root issue from another post, which was not getting much attention, and I'm wondering if anyone has a clear explanation of what's going on here. I know there are things you can do to make this work as expected, but I'm interested in knowing why it doesn't work as expected right now. It's acting as if console.log() waits until everything else is through to log anything.
The console.log shows a reference to the array, the moment you expand it it retrieves the latest array value with the latest Math.random() value.
The following demo shows this:
var test = [1,2,3];
console.log(test)
test.push(4)
console.log(test)
The preview shows the old values but when expanding the values it retrieves it using the pointer and thus gets the new values.

JavaScript array returns length 0 [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 6 years ago.
Am I being stupid here? (I'm coming from Ruby, so there might be something about Javascript arrays that I'm missing).
console.log(new_devices)
Result in console: Array[1].
console.log(new_devices.length)
Result in console: 0
The code producing this:
var sp = require('serialport');
var new_devices = [];
sp.list(function(err, ports) {
ports.forEach(function(current) {
if (current.manufacturer == "Teensyduino") {
new_devices.push(current);
}
});
});
console.log(new_devices);
console.log(new_devices.length);
When you console log arrays the console creates a reference to that array, it does not show you a snapshot of the state of the array at the point of execution.
(In your code items are appended to the list async, so when the console log is printing the list is empty.)
Consider this example:

JavaScript only logs undefined's that you define? [duplicate]

This question already has answers here:
forEach on array of undefined created by Array constructor
(5 answers)
Closed 5 years ago.
When running this code I expect this to log 5 undefined's. However I only see two. It appears that JavaScript only logs undefined's that you define yourself.
var a = [];
a[0]=undefined;
a[4]=undefined;
a.forEach((i)=>console.log(i))
This logs:
undefined
undefined
Wouldn't you expect it to log 4 undefined's?
Here is a codepen.
Quoting mdn:
forEach method executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays like your example),
From what I remember the lo-dash forEach method is different it behaves like you would expect.
It is because of forEach loop.
forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays).
--Source MDN
Example Snippet:
var a = [];
a[0] = undefined;
a[4] = undefined;
a.forEach(function(i) {
console.log(i)
})
for (var i = 0; i < a.length; i++) {
console.log(a[i] + " from for loop");
}
so javascripts arrays are basically just special obejcts that have integer keys (there are some minor differences). so you defined an object with 2 keys values. 0: undefined and 3: undefined. since theres only 2 things in the array, it only prints 2 undefineds.

Javascript Array count doesn't agree with console? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 6 years ago.
Am I being stupid here? (I'm coming from Ruby, so there might be something about Javascript arrays that I'm missing).
console.log(new_devices)
Result in console: Array[1].
console.log(new_devices.length)
Result in console: 0
The code producing this:
var sp = require('serialport');
var new_devices = [];
sp.list(function(err, ports) {
ports.forEach(function(current) {
if (current.manufacturer == "Teensyduino") {
new_devices.push(current);
}
});
});
console.log(new_devices);
console.log(new_devices.length);
When you console log arrays the console creates a reference to that array, it does not show you a snapshot of the state of the array at the point of execution.
(In your code items are appended to the list async, so when the console log is printing the list is empty.)
Consider this example:

Array updating before push - javascript [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
JavaScript console prints assigned value of variable before it has been assigned?
(8 answers)
Closed 9 years ago.
I've got a curious situation where I am trying to update an array of objects with a new object, but when I place a console.log statement before the push, it shows that the array already has the new object inside of it. Here is the basics of the code:
var array1=[{
"Name": "Lake",
"ID": "1234"
}];
var object1={
"Name": "Mountain",
"ID": "1234"
};
function testArray() {
console.log(array1);
array1.push(object1);
}
I'd eventually like to update the original array with new information if the object contains the same ID. If it does not contain the same ID, then it should be appended. This would happen with an $.each loop over array1.
I'd greatly appreciate any help. Thank you.
It is because you are doing this in a webkit broswer like Chrome and console.log() is being queued up (it's a webkit bug, it won't happen if you do it in Firefox or non-webkit broswer), and therefore it prints a later value of the array. You need to use
JSON.stringify(array1);
for a more accurate result.
If you want to update the original array with new information only when the object contains the same ID, use an if statement to check the ID:
function updateA(obj){
if(obj.ID === array1.ID){
array1.push(obj);
console.log( JSON.stringify(array1));
}
}
updateA(object1);

Categories