# Handlebars mapping commas

**URL:** <https://connective.celigo.com/t/handlebars-mapping-commas/1235>\
**Category:** Integrator.io\
**Tags:** mapper, handlebars, javascript\
**Created:** [February 4, 2021, 9:35am UTC](https://connective.celigo.com/t/handlebars-mapping-commas/1235 "2021-02-04T09:35:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hristinabendeva](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hristinabendeva](https://connective.celigo.com/u/hristinabendeva)\
**Post date:** [February 4, 2021, 9:35am UTC](https://connective.celigo.com/t/handlebars-mapping-commas/1235/1 "2021-02-04T09:35:19Z")

</div>

Hello,

I'm having trouble with placing commas when I map from one array and depending on the condition it splits the elements into two different arrays. How can I place a comma after each element except the last one in each array? Here is the handlebars mapping and the Resources that I map from:

```
{"order_info": {"shipments": [{{#each record.GetOrderResponse.OrderShipment}}{"items_info": [{{#each ../record.GetOrderResponse.OrderSkuShipment}}{{#compare OrderShipmentId "===" ../ShippmentId}}{{#each ../../record.GetOrderResponse.OrderSku}}{{#compare OrderSkuId1 "===" ../OrderSkuId2}}{"sku": "{{SkuCode}}","quantity": "{{Quantity}}"} {{/compare}}{{/each}}{{/compare}}{{/each}}],"ship_method": "{{ShippingSpeed}}","carrier": "{{ShippingMethod}}","ship_source": "{{ShippingMethod}}","ship_date": "{{ShippmentDate}}","tracking_number": "{{TrackingNumber}}",}{{#unless @last}},{{/unless}}{{/each}}]}}
```

```
{ "record": { "GetOrderResponse": { "OrderSku": [{ "Quantity": "1", "Price": "89.0000", "SkuId": "282828", "Shiped": "true", "Discount": "0.0000", "SkuCode": "1111111XL", "RegularPrice": "89.0000", "OrderSkuId1": "11111" }, { "Quantity": "1", "Price": "23.6000", "SkuId": "292929", "Shiped": "true", "Discount": "0.0000", "SkuCode": "22222222L", "RegularPrice": "23.6000", "OrderSkuId1": "22222" }, { "Quantity": "1", "Price": "23.6000", "SkuId": "272727", "Shiped": "true", "Discount": "0.0000", "SkuCode": "3333333M", "RegularPrice": "23.6000", "OrderSkuId1": "33333" }, { "Quantity": "1", "Price": "79.0000", "SkuId": "242424", "Shiped": "true", "Discount": "0.0000", "SkuCode": "44444444XS", "RegularPrice": "89.0000", "OrderSkuId1": "44444" }], "OrderShipment": [{ "ShippingMethod": "Method1", "TrackingNumber": "111111111111111111", "ShippingSpeed": "Standard", "ShippmentDate": "2020-05-26T00:00:00-05:00", "ShippmentId": "111111" }, { "ShippingMethod": "Method2", "TrackingNumber": "22222222222222222", "ShippingSpeed": "Standard", "ShippmentDate": "2020-04-20T00:00:00-05:00", "ShippmentId": "222222" }], "OrderSkuShipment": [{ "OrderSkuId2": "11111", "OrderShipmentId": "111111" }, { "OrderSkuId2": "22222", "OrderShipmentId": "111111" }, { "OrderSkuId2": "33333", "OrderShipmentId": "222222" }, { "OrderSkuId2": "44444", "OrderShipmentId": "222222" }] }}}
```

---

<div class="post-metadata">

**Author:** ![stephenbrandt](https://sea1.discourse-cdn.com/flex001/user_avatar/connective.celigo.com/stephenbrandt/32/6891_2.png) [@stephenbrandt](https://connective.celigo.com/u/stephenbrandt)\
**Post date:** [February 4, 2021, 8:43pm UTC](https://connective.celigo.com/t/handlebars-mapping-commas/1235/2 "2021-02-04T20:43:51Z")

</div>

Hi, @hristinabendeva615. I got the same error with your code. The following change seemed to work in [Dev playground](https://docs.celigo.com/hc/en-us/articles/360043040652-Advanced-field-editors-AFEs-#Create-HTTP-request-body), from:

```
{{#unless @last}},{{/unless}}
```

to:

```
{{#if @last}}{{else}},{{/if}}
```

Sorry – I can't figure out why the second block worked, but not the first. Let us know if that doesn't solve the problem for you, too.

 ![5301043caa20740200000001-30c044d28b4543db855f85629cb413e6.png](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/f/f31a8344e7a5e0424a50f9f348473a1d76c381aa.png)

---

<div class="post-metadata">

**Author:** ![hristinabendeva](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hristinabendeva](https://connective.celigo.com/u/hristinabendeva)\
**Post date:** [February 5, 2021, 9:02am UTC](https://connective.celigo.com/t/handlebars-mapping-commas/1235/3 "2021-02-05T09:02:07Z")

</div>

Hello [@stephenbrandt](https://connective.celigo.com/hc/en-us/profiles/<a class=)-Stephen-Brandt" rel="nofollow noreferrer"\>Stephen Brandt,

I have a problem with placing commas after the elements in the items\_info list not the shipments list, the JSON I uploaded for the payload had errors in it and it didn't display the problem I have, I updated the JSON and it displays the problem now, also here are some screenshots as well:

 ![5301043caa20740200000001-db584dd8810949f8ab2ed2d163780462.png](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/0/02d35d5cb37d2f4215c1a0bdc94a42198c8e6561.png)

 ![5301043caa20740200000001-f59435bee1784e6285d9d9b12b74e283.png](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/5/58ea4936d8e07e8a4c3c812085b6c06e0baa2b3f.png)

---

<div class="post-metadata">

**Author:** ![stephenbrandt](https://sea1.discourse-cdn.com/flex001/user_avatar/connective.celigo.com/stephenbrandt/32/6891_2.png) [@stephenbrandt](https://connective.celigo.com/u/stephenbrandt)\
**Post date:** [February 5, 2021, 7:34pm UTC](https://connective.celigo.com/t/handlebars-mapping-commas/1235/4 "2021-02-05T19:34:34Z")

</div>

Thanks, @hristinabendeva615. Now I'm seeing the same error. Sorry for the hasty reply yesterday.

The reason you can't rely on @last in this case is because of the #compare logic.

I ran a few tests, and the @index and @last values were all correctly maintained within the context of every #each loop.

So, at the inner nesting for "items\_info", you will have four iterations: 0, 1, 2, and 3. The @last value will be [true when the index is] 3, given your sample data. However, in only one case in your sample data does the last item match an array item's index in your #compare statement, omitting the comma.

That's an explanation, but not a solution. You'd have to check @last outside of the current #compare blocks, which could get tricky with other @../last and @../../last indices also in play.

There might be something easier and quicker, according to your business rules, like leaving blank "items\_info" objects, if that's allowed.
