I have an object (dataLayer), which can increase and decrease in length, depending on how many pages you've visited.
I want to:
- Loop through the objective and look for the property name "ecommerce".
- I specifically want to find the value of "brandcode".
- Must be in the same object group as "pagetitle: `Login Page"
Obviously, I can do dataLayer['4'].ecommerce.information. product
. However, the problem I have this that this key/value position could be at any number. EG dataLayer[0],[1],[2],[12] etc
What is the best way to loop through this object and look for this key/value pair, regardless of it's index in the object?
const dataLayer =
{ 0: { a: 123, event: 'pageload', productId: 1 }
, 1: { b: 456, event: 'pageload', productId: 3 }
, 2: { event: 'gtm.load', productId: 3 }
, 3: { event: 'gtm.load', productId: 4 }
, 4:
{ pagetitle: 'Login Page'
, ecommerce:
{ information:
{ product:
[ { brandCode: 'car', productColor: 'red' }
, { brandCode: 'car', productColor: 'green' }
, { brandCode: 'car', productColor: 'yello' }
]
} } } };
Thanks,