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

181
Visualizações
How to loop through an array of objects and see if values are empty

I'm failing to understand a concept which involves looping through an array of objects. Also, we're working with Google Tag Manager, therefore we still need to use the ES5 annotation.

My case:

We have a product array that contains multiple objects that contain very specific product data (e.g. product id, price, name etc.). It happens that some of these values are missing (e.g. undefined, null or ''). We are trying to create a rule that returns true whenever one of these values is missing and false if all are present.

I've been trying the following:

function getEmptyEcomValues () {

  var ecomDatalayer = {{ecommerce}} //the source of our array. This works so far. (see code below)

  var productArray = ecommerceEvent.products || [];

  // the problem is in this for loop. I can access the array but not all the keys and their values.

  for (var item in productArray) {
        if (productArray[item] == null || productArray[item] == undefined || productArray[item] == "") {
            return true;
      } else {
            return false;
      }

    //checkProperties(productArray) 
  
}
}

This script checks the array but keeps returning false. Even when key values are empty.

// if an array looks like this, it should return false. All values are present.

[
  {
    name: "Product1",
    id: "1234",
    price: "10.00",
    category: "video games"
  },
  {
    name: "Product2",
    id: "5678",
    price: "15.00",
    category: "video games"
  }
]

// if an array looks like this, it should return true. It has empty values.

[
  {
    name: "", //empty
    id: "1234",
    price: "10.00",
    category: "video games"
  },
  {
    name: "Product2",
    id: "5678",
    price: undefined, //undefined value
    category: "video games"
  }
]

Am I on the right track here?

about 4 years ago · Juan Pablo Isaza
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