#1 2020-02-05 03:51:48

Joker
Member
Registered: 2015-07-06
Posts: 15

Mustache - accessing an element in an array by index

Hi all,
Is it possible to access a particular element in an array by specifying it's index?

Take the array below ... I want to be able to access element 2 (i.e. the 2018 year in a zero based array).

Some general Mustache pages suggest to use syntax line this {{Report.2}}, but that doesn't seem to work.

Any ideas or suggestions would be greatly appreciated?

            "Report":
            [
                {
                    "ClassName": "TSQLBreederPerformanceLine",
                    "ID": 0,
                    "Year": 2020,
                    "Branded": 0,
                    "Breeders": 26672,
                    "BrandingRate": 0
                },
                {
                    "ClassName": "TSQLBreederPerformanceLine",
                    "ID": 0,
                    "Year": 2019,
                    "Branded": 10492,
                    "Breeders": 22577,
                    "BrandingRate": "46.4720733489835"
                },
                {
                    "ClassName": "TSQLBreederPerformanceLine",
                    "ID": 0,
                    "Year": 2018,
                    "Branded": 11273,
                    "Breeders": 22103,
                    "BrandingRate": "51.0021264081799"
                },
                {
                    "ClassName": "TSQLBreederPerformanceLine",
                    "ID": 0,
                    "Year": 2017,
                    "Branded": 10935,
                    "Breeders": 18961,
                    "BrandingRate": "57.671008913032"
                }
            ]


Cheers.

Vernon.

Offline

#2 2020-02-05 10:16:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: Mustache - accessing an element in an array by index

In the current implementation, {{Report.2}} should search for a "2" field in the "Report" object, or indeed return the Report[2] if "Report" is an array.
At least, this is what should happen.
Could you debug and see why TSynMustacheContextVariant.GetValueFromContext() doesn't reach TDocVariant.IntGet() which should call TDocVariantData.RetrieveValueOrRaiseException() then TDocVariantData.GetValueIndex() to detect it is a TDocVariant array and return the 3rd item?

Note that this is not a standard mustache syntax at all.

Offline

#3 2020-02-05 21:29:57

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Mustache - accessing an element in an array by index

Thanks ab.

I am not using the latest version of Mormot, so I will update and try again.

Cheers.

Vernon.

Offline

Board footer

Powered by FluxBB