0

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.

4
  • you can't use wildcards there, $.each simply loops over an array or an object's keys. You'll have to do the filtering using something else, such as an if statement. Commented Jun 2, 2014 at 15:39
  • 2
    If possible, it would be much easier to change your JSON structure. Can you change the format to contain an array of terms instead of each having it's own property? Commented Jun 2, 2014 at 15:40
  • 1
    FYI, value will be the object to iterate over, term is the property name. Check the documentation: api.jquery.com/jquery.each Commented Jun 2, 2014 at 15:43
  • I get that JSON as a result of an API request so yep, I could re-arrange the whole thing backend-side. Since I'm new to web dev I just assumed the API was right and the problem was me though :] Commented Jun 2, 2014 at 15:51

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

4 Comments

That's interesting, thanks. If I decide to rearrange the API response, would you say is better to do it client-side as in your example or server-side?
@laurids this works on any browser older than IE9 and if you polyfill on any browser
actually I was more concerned about which one is the "good practice" rather than portability ;)
This approach uses ES5 standards. If you want to use jQuery, Underscore or lodash it will depend on the library to provide "portability" probably about the same coverage these days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.