In place for going flat, It would be easy to create an array of product details i.e. somewhat similar to below...
digitalData = {
comparisonInfo: [{
productName: "xxx1",
comparisonDisp: 5,
comparisonBrand: "brand1",
comparisonPrice: "price1"
},
{
productName: "xxx2",
comparisonDisp: 4.5,
comparisonBrand: "brand2",
comparisonPrice: "price2"
},
{
productName: "xxx3",
comparisonDisp: 5.5,
comparisonBrand: "brand3",
comparisonPrice: "price3"
},
{
productName: "xxx4",
comparisonDisp: 4,
comparisonBrand: "brand4",
comparisonPrice: "price4"
}
]
}
Then use the forEach loop to iterate through it; change the key name in the code below to get value for different keys. eg. to get value for "comparisonBrand" comparisonBrand"
var foo = []
digitalData.comparisonInfo.forEach(function(e) {
foo.push(e.comparisonBrand);
console.log(foo);// remove this line when use in dtm.
});
Good Luck