Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
JavaScript unable to verify empty value in an array

I have an array of google map markers and I'm removing some of them using the delete method. The function I use is as below;

function removeMarker(markerOrder) {
   markers[markerOrder].setMap(null);
   delete markers[markerOrder];
   console.log(markers);
   counter--;
}

I cannot change delete as function because I need to keep the markers array order.

If I delete a value, in the console it is shown as replaced with empty as value.

So when all the markers are deleted from he markers array, I see in the console:

(4) [empty × 4]

I'm trying to verify if the array has all its values empty but it seems like the empty I see in console is neither null, "" nor undefined.

Here is my code:

function allEmpty(arr) {
    for (var i = 0; i < arr.length; i++) {
       if (arr[i] !== null) {//Not working
       //if (arr[i].length !== 0) {//Not working
       //if (typeof(arr[i]) !== 'undefinded') {//Not working
       //if (arr[i] !== '') {//Not working
           return false;
       }
   }
   return true;
}

Any help please? Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

To check if the array is empty, you can use .some to help you, for example:

function allEmpty(arr) {
    return !arr.some(el=>el)
}

const arr = [1, 2, 3, 4, null, 6]
console.log(allEmpty(arr))
//false

const arr2 = ['', null, undefined]
console.log(allEmpty(arr2))
//true
about 4 years ago · Juan Pablo Isaza Relatório

0

delete sets the element to undefined, not null. So use that comparison in your loop.

function allEmpty(arr) {
    for (var i = 0; i < arr.length; i++) {
       if (arr[i] !== undefined) {
           return false;
       }
   }
   return true;
}

You can also use the every() method:

function allEmpty(arr) {
    return arr.every(el => el === undefined);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda