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?