How to declare that an object in a Javascript array is empty

Asked

Viewed 73 times

1

I made a program to simulate the administration of a parking lot. The console.log presents the amount of vacancies and which cars are in this vacancy. However I did 4 vacancies with 4 cars, and I wanted to make 7 vacancies with 3 of them being empty for the console.log present "vacancy 5 is empty" if empty.

Code:

let vagas = ['Fiat, Placa ABC-0123' , 'HB20, Placa ACB-0213' , 'Palio, Placa BCA-3210' , 'Kadett, CBA-2031', '']

console.log(`No estacionamento tem ${vagas.length} vagas`)
for (let lugar = 0; lugar < vagas.length; lugar++){
    console.log(`Na vaga ${lugar + 1} está o Automóvel ${vagas[lugar]} `)  
}
  • When there is no car in the vacancy, Voce wants to show on the console something like A vaga 5 está vazia, would that be? because I didn’t quite understand your question.

  • exact! pardon I’ll edit the question but that’s right!

2 answers

4

Make a if to verify that the value is equal to '' already help your case.

It would look like this in my example below:

let vagas = ['Fiat, Placa ABC-0123' , 'Tie Fighter, Placa kill-rebels', '', 'HB20, Placa ACB-0213' , 'Palio, Placa BCA-3210' , 'Kadett, CBA-2031', '', '']

console.log(`No estacionamento tem ${vagas.length} vagas`)
for (let lugar = 0; lugar < vagas.length; lugar++){
    if (vagas[lugar] === '')
     console.log(`A vaga ${lugar + 1} está vazia`)
    else
     console.log(`Na vaga ${lugar + 1} está o Automóvel ${vagas[lugar]}`)
   
}

Where when vagas[lugar] === '' is true, let’s show a different message on the console, indicating that the place is empty.

2


Bearing in mind that empty strings ("") in Javascript are values falsy, you can use vagas[lugar] directly on if without the need for any kind of comparison.

This happens because the if, when testing its expression, checks whether the value can be converted to the corresponding boolean. As Boolean("") (note the empty string in the first argument) evaluates to false, in case the vacancy is "empty", it will not "enter the if".

Learn more about values Truthy and falsy here.

Thus:

if (vagas[lugar]) {
  // A vaga está usada...
} else {
  // A vaga está vazia...
}

The advantage of this is that your array may contain other values falsy, which will also be considered as an empty vacancy. For example, null, undefined, false etc..

We therefore have:

let vagas = [
  null, undefined, '', // <--- Todos os valores desta linha serão vistos como vaga vazia.
  'Fiat, Placa ABC-0123' , 'HB20, Placa ACB-0213' ,
  'Palio, Placa BCA-3210', 'Kadett, CBA-2031'
];

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

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


Another option is to compare the value literally with an empty string, as suggested by another answer.

  • Very good, but you know what I noticed? You should have one if for when " " (with space), if not, we will have an unexpected XD behavior. I would use regex, but that’s not the case :)

  • @Cmtecardeal, really, but if it were so, you would have to do a check for each amount of different spaces. You could also do " ".trim(), but I think it’s too much. Anyway, it’s probably not a requirement of AP.

Browser other questions tagged

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