Javascript / jquery find id number in json multidimensional object and use object where id is as object

How can I say that. Let's start with an example of my object:

"data": {
    "events": [
        {
            "event_id": 12345,
            "event_ts_begin": 1358931738000,
            "event_ts_end": 1358931748000,
            "event_severity": "minor",
            "event_code": 23,
            "event_desc": "Completed",
            "event_type": "normal",
            "event_text": "Completed, blah on blah blah blah..."
        },
        {
            "event_id": 12346,
            "event_ts_begin": 1358931738000,
            "event_ts_end": 1358931748000,
            "event_severity": "minor",
            "event_code": 23,
            "event_desc": "Completed",
            "event_type": "normal",
            "event_text": "Completed, blah on blah blah blah..."
        },
        {
            "event_id": 12347,
            "event_ts_begin": 1358931738000,
            "event_ts_end": 1358931748000,
            "event_severity": "minor",
            "event_code": 23,
            "event_desc": "Completed",
            "event_type": "normal",
            "event_text": "Completed, blah on blah blah blah..."
        }
    ]
}

      

What I want to do is how to find out where event_id

12346 is in this multidimensional object, and use that particular dataset as an object in its own right. Taking it out somehow and defining a temporary variable as this object, it will be equal to:

var tempObj = {
            "event_id": 12346,
            "event_ts_begin": 1358931738000,
            "event_ts_end": 1358931748000,
            "event_severity": "minor",
            "event_code": 23,
            "event_desc": "Completed",
            "event_type": "normal",
            "event_text": "Completed, blah on blah blah blah..."
        };

      

In some cases, an example of an object with multiple objects may have more than 1000 objects or more in it. So looping over them and rebuilding the option doesn't seem like a great idea. So I hope I can get some feedback to come up with a sensible solution.

Is there a way to find the specific index number of that specific object to use as a means of defining temp var?

+3


source to share


1 answer


You can use the jQuery.grep function to find elements in an array.



var tmpObj = $.grep(data.events, function(obj){
    return obj.event_id == '12347'
});

      

+4


source







All Articles