Is it possible to create a custom array based on 2 other arrays?

Silver Contributor
Silver Contributor

Hello,

We have a vendor who would like an array of skus and the matching prices constructed in a specific way.

We have the skus and the prices in separate variables. Is it possible to use these arrays to construct another array in TIQ?

Existing variables on the page:

sku: ["925913", "893200", "893201"]
sku_price: ["64.99", "74.99", "89.99"]

 

New variable to be created for this specific vendor:

new_array: ["925913";"64.99"|"893200";"74.99"|"893201";"89.99"]

 

Thanks for any help,

Jason

 

2 REPLIES 2

Is it possible to create a custom array based on 2 other arrays?

Tealium Expert
Tealium Expert

The first thing to say is that the target structure isn't an array, at least not a valid JS array, it looks more like a delimited string, so the following is an attempt to arrive at that outcome:

 

var sku_combined = [];
if(b.sku.length == b.sku_price.length){
  for(var i = 0; i < b.sku.length; i++){ 
    b.sku_combined.push(JSON.stringify(b.sku[i]) + ";" + JSON.stringify(b.sku_price[i]));
  }
}
b.sku_combined = sku_combined.join("|");

 

 

Which will set the "sku_combined" UDO variable to the following string:

"925913";"64.99"|"893200";"74.99"|"893201";"89.99"

If the vendor really needs that wrapped in square brackets, you can do it as part of that last operation, but to me it feels a bit weird to start wrapping it in what looks like a JS structure when it's not valid JS...

Is it possible to create a custom array based on 2 other arrays?

Silver Contributor
Silver Contributor
Thanks for this! I will double check the documentation on the brackets and test this out.
Public