JavaScript array returns length 0 [duplicate] - javascript

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:

Related

JS Array: values get lost in JSON.stringify() [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 11 months ago.
I am trying out JS today and ran into the issue that the values of an array get lost when I convert it. The length of the JSON string is correct.
<script>
console.log(arrayTmpre); // returns the correct array to console
var testtwo = JSON.stringify(arrayTmpre); // should return the array as JSON readable string
console.log(testtwo); // the result looks like this: [null,null,null] The values are missing!
</script>
This is the output in the console
Thanks in advance!

Unable to iterate throug JS array of objects [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Chrome DevTools - inconsistent array length
(1 answer)
Closed 4 years ago.
When i see my book object in console it shows array but when i try to iterate it does not iterartes in loop.
console.log(book);// logs array of length 6
console.log(typeof book);// logs -> object
console.log(book.length);// logs 0
//this for loop does not iterates.
for(let i of book)
{
//logic
}
Please help me how to iterate?
my book object looks like below on console.
let i of book is wrong. Since it is an array of object, try to do below.
book.forEach( function (bookItem)
{
Your logic
});

logging javascript object properties [duplicate]

This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 5 years ago.
Please share your thoughts regarding below outputs.
var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';
var a = {};
console.log(a); // How javascript is aware of property here that is defined below.
a.name = 'triven';
I think you're looking for console.dir().
console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.
The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));

Chrome developer tool Array length [duplicate]

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.

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:

Categories