3

Uh hmm how can I word this. Lets start of 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 some how find out where event_id 12346 is in this multidimensional object and use that particular data set as an object by itself. Somehow taking it out and defining a temporary variable as that object so it would 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..."
        };

The sample object above with the multiple objects in it, can be 1,000+ objects or more in some cases. So looping over them and rebuilding an option doesn't sound like a great idea. So I am hoping I can get some feedback to come up with a sane solution.

Is there a way I can find the particular index number of that particular object to use it as a means of defining the temp var?

1
  • Sorry but you must loop :( Check some phpjs.org about arrays to get an optimized code. Commented Jan 24, 2013 at 2:24

1 Answer 1

4

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

var tmpObj = $.grep(data.events, function(obj){
    return obj.event_id == '12347'
});
Sign up to request clarification or add additional context in comments.

Comments

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.