them with a for loop.
My test code:
const cars = {};
cars[0] = "V2";
cars[1] = {};
cars[1]["p"] = "1.0";
cars[2] = {};
cars[2]["p"] = "1.1";
console.log(JSON.stringify(cars, null, 2));
console.log(cars[1]["p"]);
let text = "";
for (let i = 1; i < cars.length - 1; i++) {
text += cars[i]["p"] + "<br>";
}
the console log shows the correct object and outputs as well the value of "p" from the 2nd element
but the for loop doesnt output anything at all. the for loop did work with a simple non multi level object
what do i miss here/did i do wrong ?
for (let i = 0; i < cars.length; i++) { text += cars[i]["p"] + "<br>"; } Index starts from 0 so i should starts from 0 Also the length starts from one so it's either < length or <= length - 1 not < length - 1 We have cars.length = 2 So cars.length - 1 = 1 i starts from 1. so this equation is false: i < cars.length - 1
https://en.wikipedia.org/wiki/Off-by-one_error
doesnt work either i started with 1 since i don't need the 1st element of the object in this part
Обсуждают сегодня