查询javascript对象 [英] query javascript object

查看:80
本文介绍了查询javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串过来并被绑定到一个javascript对象

Ive got a JSON string coming over and being assinged to a javascript object

{
   "results":[
      {
        "id":"460",
        "name":"Widget 1",
        "loc":"Shed"
      },{
        "id":"461",
        "name":"Widget 2",
        "loc":"Kitchen"
      }]
}

有没有办法在javascript中查询这些数据,这样我就可以搜索ID为460并获取名称和loc返回(除了循环遍历整个对象)?我有jQuery和Prototypejs可供使用。

Is there a way to "query" this data in javascript so I could search for an ID of 460 and get name and loc returned (other than just looping through the whole object)? I've got jQuery and Prototypejs available to use.

推荐答案

DEMO

JavaScript数组内置过滤方法

JavaScript arrays have a built-in filter method:

var valuesWith460 = obj.results.filter(function(val) {
    return val.id === "460";
});

(支持旧浏览器,你想要从上面的链接中获取垫片)

(to support older browsers you'll want to grab the shim from the link above)

这篇关于查询javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆