Skip to content Skip to sidebar Skip to footer

Check For A Value In A Json Object

I need to find out if an id occurs in my JSON object, for example: { 'requested': '2009-07-25T14:12:25+01:00', 'channels': [ {'id': 1, 'name': 'General', 'created': '2009-0

Solution 1:

Try this:

var id = 2,
    found = false;
for (var i=0; i<channels.length; i++) {
    if (channels[i].id == id) {
        found = true;
        break;
    }
}

Solution 2:

Or more succinctly and probably faster :

var id = 2; 
for (var found, i = channels.length; i && !(found = channels[--i].id === id);)
  ;

Post a Comment for "Check For A Value In A Json Object"