Похожие чаты

Hi. I have a question/issue regarding multilevel objects and using

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 ?

3 ответов

18 просмотров

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

Hitmare-Chelien Автор вопроса
Sina
for (let i = 0; i < cars.length; i++) { text += ...

doesnt work either i started with 1 since i don't need the 1st element of the object in this part

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта