Unable to iterate throug JS array of objects [duplicate] - javascript

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
});

Related

How to use filter to compare to arrays to find missing object? [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.

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!

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)));

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 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