Reputation: 63
there
I have a JSON response as below:
"log": [{
"a": 0.40,
"b": "ED",
}, {
"c": 82,
"d": "ABC",
}, {
"e": 36,
"f": 23,
}, {
"g": 12,
"h": 40,
}
]
I need to count a number of lists in a 'log' object to be able to address the last one and find a specific element in it. The response is dynamic and has different amount of lists in it (in this case 4)
I tried log[-1], and examples form js-arrays.feature as in the link below: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature#L83
It is easy to find a number of elements in a list, but I frequently have variable amount of lists and I cant make it work Many Thanks,
Upvotes: 1
Views: 7661
Reputation: 1455
Take into account that log[-1] would return you undefined due to there is no index -1
in the array. For get the number of elements in your array you must do
log.length
Also about the link you've posted
log[log.length-1]; //This will return the last element of the array in this case { "g": 12, "h": 40, }
Upvotes: 2