What is the correct/idiomatic way to loop through JSON objects that only start with a certain pattern?
Example: say I have a JSON like
{
"END": true,
"Lines": "End Reached",
"term0": {
"PrincipalTranslations": {
// nested data here
}
},
"term1": {
"PrincipalTranslations": {
// more nested data here
}
}
}
I only want to access the PrincipalTranslations
object and I tried with:
$.each(translations, function(term, value) {
$.each(term, function(pos, value) {
console.log(pos);
});
});
Which doesn't work, possibly because I can't loop through the END
and Lines
objects.
I tried to go with something like
$.each(translations, function(term, value) {
$.each(TERM-THAT-STARTS-WITH-PATTERN, function(pos, value) {
console.log(pos);
});
});
using wildcards, but without success. I could try to mess up with if
statements but I suspect there is a nicer solution I'm missing out on. Thanks.
How I would search for a property in an object it is like this:
If you're only interested in the
PrincipalTranslations
-objects, following would do the trick:JSFiddle