Answered
Removing comma from filtered array
Hi
I'm having a problem when filtering and array with handlebars, if the element that is filtered out of the array is the last one it adds a comma in the filtered array at the last element. Here is the input message:
{
"id": 130,
"customer_id": 217,
"date_created": "Mon, 23 May 2022 12:49:37 +0000",
"date_modified": "Mon, 23 May 2022 12:49:38 +0000",
"date_shipped": "",
"status_id": 11,
"status": "Awaiting Fulfillment",
"subtotal_ex_tax": "2031.0000",
"subtotal_inc_tax": "2233.0816",
"external_id": null,
"external_merchant_id": null,
"tax_provider_id": "BasicTaxProvider",
"customer_locale": "en",
"store_default_currency_code": "USD",
"store_default_to_transactional_exchange_rate": "1.0000000000",
"custom_status": "Awaiting Fulfillment",
"Products": [
{
"id": 55,
"order_id": 130,
"product_id": 1108,
"variant_id": 31879,
"order_address_id": 31,
"name": "RR-127 Test product [3 swatches]",
"name_customer": "RR-127 Test product [3 swatches]",
"name_merchant": "RR-127 Test product [3 swatches]",
"sku": "RR-127-OR-32",
"upc": "",
"type": "physical",
"base_price": "999.0000",
"price_ex_tax": "999.0000",
"price_inc_tax": "1098.4939",
"price_tax": "99.4939",
"base_total": "999.0000",
"total_ex_tax": "999.0000",
"total_inc_tax": "1098.4939",
"total_tax": "0.9800",
"weight": "1.0000",
"width": "0.0000",
"height": "0.0000",
"depth": "0.0000",
"quantity": 1
},
{
"id": 56,
"order_id": 130,
"product_id": 2546,
"variant_id": 31842,
"order_address_id": 31,
"name": "Test product [10 swatches]",
"name_customer": "Test product [10 swatches]",
"name_merchant": "Test product [10 swatches]",
"sku": "RR-127-1-OR-1-1-1-1-1-2",
"upc": "",
"type": "physical",
"base_price": "999.0000",
"price_ex_tax": "999.0000",
"price_inc_tax": "1098.4939",
"price_tax": "99.4939",
"base_total": "999.0000",
"total_ex_tax": "999.0000",
"total_inc_tax": "1098.4939",
"total_tax": "0.9800",
"weight": "1.0000",
"width": "0.0000",
"height": "0.0000",
"depth": "0.0000",
"quantity": 1
},
{
"id": 57,
"order_id": 130,
"product_id": 2545,
"variant_id": 31794,
"order_address_id": 31,
"name": "RR-174 Test product",
"name_customer": "RR-174 Test product",
"name_merchant": "RR-174 Test product",
"sku": "RR-174",
"upc": "",
"type": "physical",
"base_price": "33.0000",
"price_ex_tax": "33.0000",
"price_inc_tax": "36.0938",
"price_tax": "3.0938",
"base_total": "33.0000",
"total_ex_tax": "33.0000",
"total_inc_tax": "36.0938",
"total_tax": "0.0300",
"weight": "1.0000",
"width": "0.0000",
"height": "0.0000",
"depth": "0.0000",
"quantity": 1
},
{
"id": 58,
"order_id": 130,
"product_id": 2569,
"variant_id": 31941,
"order_address_id": 0,
"name": "__Placeholder",
"name_customer": "__Placeholder",
"name_merchant": "__Placeholder",
"sku": "placeholder",
"upc": "",
"type": "digital",
"base_price": "0.0000",
"price_ex_tax": "0.0000",
"price_inc_tax": "0.0000",
"price_tax": "0.0000",
"base_total": "0.0000",
"total_ex_tax": "0.0000",
"total_inc_tax": "0.0000",
"total_tax": "0.0000",
"weight": "0.0000",
"width": "0.0000",
"height": "0.0000",
"depth": "0.0000",
"quantity": 1
}
]
}
Here is the handlebars tranformation:
{
"id": {{id}},
"customer_id": {{customer_id}},
"date_created": "{{date_created}}",
"date_modified": "{{date_modified}}",
"status": "{{status}}",
"Products": [ {{#each Products}}{{#compare name "!=" "__Placeholder"}}
{
"id": {{id}},
"order_id": {{order_id}},
"product_id": {{product_id}},
"variant_id": {{variant_id}},
"order_address_id": {{order_address_id}},
"name": "{{name}}",
"sku": "{{sku}}",
"quantity": {{quantity}}
}{{#if @last}}{{else}},{{/if}}{{/compare}}{{/each}}
]
}
And here is the output message:
{
"id": 130,
"customer_id": 217,
"date_created": "Mon, 23 May 2022 12:49:37 +0000",
"date_modified": "Mon, 23 May 2022 12:49:38 +0000",
"status": "Awaiting Fulfillment",
"Products": [
{
"id": 55,
"order_id": 130,
"product_id": 1108,
"variant_id": 31879,
"order_address_id": 31,
"name": "RR-127 Test product [3 swatches]",
"sku": "RR-127-OR-32",
"quantity": 1
},
{
"id": 56,
"order_id": 130,
"product_id": 2546,
"variant_id": 31842,
"order_address_id": 31,
"name": "Test product [10 swatches]",
"sku": "RR-127-1-OR-1-1-1-1-1-2",
"quantity": 1
},
{
"id": 57,
"order_id": 130,
"product_id": 2545,
"variant_id": 31794,
"order_address_id": 31,
"name": "RR-174 Test product",
"sku": "RR-174",
"quantity": 1
},
]
}
As you can see it adds a comma at the last element because it's not the last one in the original array and the expression "{{#if @last}}{{else}},{{/if}}" only avoids adding a comma after the last element, is there any way to avoid this by using handlebars?
0
Comments
Hi,
We cannot solve this use case using handlebar expressions as the @last, @first, @index works based on the original array and not the filtered array. I would request you guys to make use of postMap hook to solve this usecase.
Thanks.
Hi Hristina,
As Niranjan as mentioned, we cannot solve this issue using just handlebars. Here's another workaround which might be helpful:
Handlebars Template:
In the import mappings, you can use this checkbox to ignore the empty object which we've added to the Products array.
Hristina Bendeva I have moved this post under Troubleshoot custom flows.
Please sign in to leave a comment.