"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..."
}
]
}
我想要做的是如何找出event_id
12346在这个多维对象中的位置,并将该特定数据集单独用作对象。以某种方式将其取出并将临时变量定义为该对象,因此它将等于:
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..."
};
上面的示例对象中包含多个对象,在某些情况下可以是1,000多个对象或更多。因此,循环它们并重建选项并不是一个好主意。所以我希望我能得到一些反馈来提出一个理智的解决方案。
有没有办法可以找到该特定对象的特定索引号,以将其用作定义temp var的方法?
答案 0 :(得分:4)
您可以使用jQuery.grep函数查找数组中的元素。
var tmpObj = $.grep(data.events, function(obj){
return obj.event_id == '12347'
});