Check value in JSON object

I need to find out if id exists in my JSON object, for example:

{ "requested": "2009-07-25T14:12:25+01:00", "channels": [ {"id": 1, "name": "General", "created": "2009-07-25 14:00:02"} ] } 

In particular, I need to check if my id (say 2) is in the channels. i .id . How can I do that?

+2
source share
2 answers

Try the following:

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

Or more briefly and probably faster:

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

Source: https://habr.com/ru/post/905102/


All Articles