cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Find Array index from object value in the array object getindex?

sdelano
New Contributor II

I have a json file with an array designed like below. I donโ€™t have control over the number of or order of address entries. I have tried $.Address.findIndex(x => x == โ€˜Homeโ€™) and $.Address ? (value.AddrType = Home) among other expression language statements.

I would like to query the array by AddrType and get the index of the matching array entry record. Has anyone done something similar. We are attempting to normalize<<>>denormalize records based on types to denormalized fields that correspond to the types.

{fnam:โ€œJoeโ€,Lname:โ€œLongโ€,
Address:[
{
โ€œAddrTypeโ€: โ€œMailingโ€,
โ€œStreetAddr1โ€: โ€œ123 Prospect Streetโ€,
โ€œCityโ€: โ€œWalthamโ€,
โ€œStateโ€: โ€œMAโ€,
โ€œZipcodeโ€: โ€œโ€
},
{
โ€œAddrTypeโ€: โ€œMainโ€,
โ€œStreetAddr1โ€: โ€œ123 Baystate Streetโ€,
โ€œCityโ€: โ€œโ€,
โ€œStateโ€: โ€œโ€,
โ€œZipcodeโ€: โ€œโ€,
}
]

2 REPLIES 2

tstack
Former Employee

Is this the exact expression that you tried?

If so, it wonโ€™t work because you missed the โ€˜AddrTypeโ€™ field reference. Something like this should work:

$.Address.findIndex(x => x.AddrType == "Home")

sdelano
New Contributor II

Works perfectly thank you.