Skip to content Skip to sidebar Skip to footer

Loop Through JSON Objects That Only Start With A Certain Pattern

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',

Solution 1:

If you're only interested in the PrincipalTranslations-objects, following would do the trick:

$.each(translations, function(term, value) {
    if (value.PrincipalTranslations !== undefined) {
        console.log(value.PrincipalTranslations);
    }
});

JSFiddle


Solution 2:

How I would search for a property in an object it is like this:

var obj1 ={ /* your posted object*/};


// navigates through all properties
var x = Object.keys(obj1).reduce(function(arr,prop){
// filter only those that are objects and has a property named "PrincipalTranslations"
    if(typeof obj1[prop]==="object" &&  Object.keys(obj1[prop])
        .filter(
            function (p) {
                return p === "PrincipalTranslations";})) {
                     arr.push(obj1[prop]);
                }
    return arr;
},[]);

console.log(x);

Post a Comment for "Loop Through JSON Objects That Only Start With A Certain Pattern"