Reply
Highlighted
Visitor
Posts: 6
Registered: ‎09-22-2015

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

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

 

Reply
0 Kudos
Occasional Contributor
Posts: 120
Registered: ‎12-11-2015

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

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...

Visitor
Posts: 6
Registered: ‎09-22-2015

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

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