0

I have json data like

{
"itinerary": {
    "carrier": "9W",
    "refundable": false,
    "dept": {
        "flightList": [
            {
                "from": {
                    "date": "200713",
                    "code": "KTM",
                    "terminal": null,
                    "time": "1430"
                }
            },
            {
                "from": {
                    "date": "210713",
                    "code": "BOM",
                    "terminal": "1B",
                    "time": "0440"
                }
            },
            {
                "from": {
                    "date": "210713",
                    "code": "AMD",
                    "terminal": null,
                    "time": "1420"
                }
            }
        ]
    }
}

} Now i want to display 'KTM' and 'AMD' from json which is first and last array of itinerary.dept.flightList Now I could get first index like this {itinerary.dept.flightList[0].from.code} but i couldn't get last one. Could anyone help me to get last index of itinerary.dept.flightList without using loop

user1632223
  • 31
  • 2
  • 7

2 Answers2

1

It will be length of the array minus 1.

itinerary.dept.flightList[itinerary.dept.flightList.length-1].from.code
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • Thanx Orangepill, But i tried this before this code didn't worked. I tried like {itinerary.dept.flightList[itinerary.dept.flightList.length-1].to.code} to get 'AMD' from that json but this didn't worked. any other way to get it ? – user1632223 Jul 21 '13 at 04:52
  • @user1632223 Sorry I missed the flightList level of the composed object... updated answer to show usave to get the from code for the last element in the supplied example – Orangepill Jul 21 '13 at 04:58
  • @user1632223 I updated again after testing the code... should be on the money now. – Orangepill Jul 21 '13 at 05:04
  • I tried this code " itinerary.dept.flightList[itinerary.dept.flightList.length-1].from.code "before i post this question, this code didn't worked so i post this question. Does it depend on dust version currently i am using dust-full-1.2.0.js ? – user1632223 Jul 21 '13 at 05:32
  • I'm answering based on how you access a property with this composition for javascript in general. The posted example show how to get the property from the json object posted on the question. Nothing specific to dust. – Orangepill Jul 21 '13 at 05:38
  • I am sorry, i forgot to mention dust js template in my comments. But i have mention it in my question. Anyway thank you for your answer. So now do you have any solution for this in dust js. – user1632223 Jul 21 '13 at 05:59
  • @user1632223 you may have to right a custom helper for this unfortunately :( – Orangepill Jul 21 '13 at 07:20
0

Here is a way to do it using the {@if}, {$idx}, and {$len} helpers.

{#itinerary.dept.flightList}
    {@if cond="{$idx} == 0 || {$idx} == ({$len} - 1)"}
        {from.code}
    {/if}
{/itinerary.dept.flightList}

You can see a working example in this fiddle: http://jsfiddle.net/jamsyoung/Ucpf5/

More information is also available at:

Community
  • 1
  • 1
jamie young
  • 1,857
  • 1
  • 14
  • 12