How to count from "1" instead of "0" in a Javascript array

Asked

Viewed 136 times

1

I’m studying vectors and I used cars and parking spaces for example.

But the console.log starts from the 0 and I wanted him to tell from the 1 and even so present 4 vacancies "1, 2 , 3, 4".

Code:

let vagas = ['Audi', 'HB20', 'Lamborghini', 'Maserati']

console.log(`No estacionamento tem ${vagas.length} vagas`)

for (let lugar in vagas) {
    console.log(`na vaga ${lugar} está o Automóvel ${vagas[lugar]}`)
}

The exit follows:

na vaga 0 está o Automóvel Audi
na vaga 1 está o Automóvel HB20
na vaga 2 está o Automóvel Lamborghini
na vaga 3 está o Automóvel Maserati

I wanted it to count "1 ,2 ,3 ,4".

  • 1

    PS: for...in should not be used for iteration in an array where the order is important, since it iterates it in an arbitrary order - fountain: MDN Web Docs | for...in

3 answers

5

Do not use the for..in, which should be used to iterate on the keys enumerable of an object. Use a for normal and, at the time of printing, add 1 to the counter (i).

Something like that:

let vagas = ['Audi', 'HB20', 'Lamborghini', 'Maserati'];

console.log(`No estacionamento tem ${vagas.length} vagas`);

for (let i = 0; i < vagas.length; i++) {
  console.log(`Na vaga ${i + 1} está o Automóvel ${vagas[i]}`);
}

Javascript arrays are objects that have enumerable elements, always starting from the zero index. Do not try to modify this default behavior of the language because it will be gambiarra. If necessary start the showing off from 1, do it only at the time of displaying.

  • Right, "If necessary start the display from 1, do it only at the time of display" as?

  • Adding at the time of display, as I did in the previous code. Note that I only disappeared at the time of printing (logo in console.log).

3


Adding 1, that’s it. Because you don’t want to count from 1, you just want to present the element as vague 1.

let vagas = ['Audi', 'HB20', 'Lamborghini', 'Maserati'];
console.log(`No estacionamento tem ${vagas.length} vagas`);
for (let lugar in vagas) console.log(`na vaga ${parseInt(lugar) + 1} está o Automóvel ${vagas[lugar]}`);

Another way is to use one for crude, which I find more suitable for your case:

let vagas = ['Audi', 'HB20', 'Lamborghini', 'Maserati'];
console.log(`No estacionamento tem ${vagas.length} vagas`);
for (let i = 0; i < vagas.length; i++) console.log(`na vaga ${i + 1} está o Automóvel ${vagas[i]}`);

I put in the Github for future reference.

One more possibility is to create a array sparse explicitly telling which keys are, but this is a bit dangerous and has to know how to manage, for example, have to count how many elements have instead of picking the size:

let vagas = {1 : 'Audi', 2 : 'HB20', 3 : 'Lamborghini', 4 : 'Maserati'};
console.log(`No estacionamento tem ${Object.keys(vagas).length} vagas`);
for (let lugar in vagas) console.log(`na vaga ${lugar} está o Automóvel ${vagas[lugar]}`);

  • Still has the possibility to use the function foreach(), in case it would result in something like this: vagas.forEach((vaga, lugar) => console.log(\In the vacancy ${place + 1} is the Automobile ${vacancy}`))`;

1

Do so, order from 1 and ready.

var let = [ 
{ nome: 'Audi', num: 1     },
{ nome: 'HB20', num: 2     },
{ nome: 'Lamborghini', num: 3    },
{ nome: 'Maserati', num: 4    }
];

for (let i = 1; i < let.length; i++) {
console.log(`Na vaga ${i} está o Automóvel ${let[i]}`);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.