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.

Community
  • 1
  • 1
laurids
  • 931
  • 9
  • 24
  • 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. – Kevin B Jun 02 '14 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? – Rory McCrossan Jun 02 '14 at 15:40
  • 1
    FYI, `value` will be the object to iterate over, `term` is the property name. Check the documentation: http://api.jquery.com/jquery.each/ – Felix Kling Jun 02 '14 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 :] – laurids Jun 02 '14 at 15:51

2 Answers2

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

Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
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);
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • 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 Jun 02 '14 at 15:54
  • @laurids this works on any browser older than IE9 and if you polyfill on any browser – Dalorzo Jun 02 '14 at 15:55
  • actually I was more concerned about which one is the "good practice" rather than portability ;) – laurids Jun 02 '14 at 15:59
  • 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. – Dalorzo Jun 02 '14 at 16:02