I'm trying to set up a customJS variable in GTM. Basically it finds a corresponding value to a key from a dynamic array.
The code I wrote is as follows (for the example I set up an array like this):
function () {
var items = [{
finalPrice: 20,
price: 30,
productId: "7788",
quantity: 1,
sku: "1",
title: "Apple"
},
{
finalPrice: 10,
price: 15,
productId: "5566",
quantity: 1,
sku: "2",
title: "Orange"
}
];
return items.find(
function(i) {
i.title === "Orange"}).finalPrice;
}
What I'd like to get back as the result is the finalPrice of the array item where the title is "Orange", but for some reason I get undefined back, and I can't figure out why. Thank you in advance, I'd greatly appreciate the help!
The problem is not about GTM. You forgot return:
return items.find(
function(i) {
return i.title === "Orange"
}).finalPrice;