Ram Singh Ram Singh - 3 months ago 7x
JSON Question

get max length of characters of a value from json jquery without loop?

I have a json, i want to get maximum count of characters from any value under a specific tag without each or for loop, is it possible?

"Language": {
"ru": [
"from": "set",
"to": 444
"from": "sc",
"to": 222
"he": [
"from": "trdsss",
"to": 3333333
"from": "ahsss",
"to": 55555

Let me explain the exact requirement, i have to get maximum length of string under "RU", like RU has maximum lenght is 3 i.e. "Set" i don't want to get the string, just want to get the max. length. like for "HE" it is 6 for "trdsss".

i can do it by looping, but i don't want to go in that way, is there any other way i can get this. please help me.. how i can do it.


No, you can't avoid looping. You can avoid actually writing a for loop by (say) using Array#reduce or Array#forEach, but something, somewhere, is going to have to loop.

When I asked why you wanted to avoid looping, you said

because this method will call number of times, so i am trying skipping looping

and when I asked if those repeated calls would be for the same data, you said

yes data will be same it comes from json once

So if it's important to avoid repeating the loops (it probably isn't, but it could be), you'd just loop the first time and then remember the result so you could reuse it in subsequent calls. That's straightforward to write.