Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!