jquery: how to get a row of json data from the json data list by id, etc?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 23:25:00

问题


I have a json data like this below, how can I select the row of data by its id or by its email, etc?

[ 
Object { id="40", full_name="lau", email="kok@com", more...},  
Object { id="39", full_name="Kok", email="lau@lauthiamkok.net", more...},  
Object { id="5", full_name="Alice", email="alice@umbrellacorp.com", more...},  
4 more... 
]

if I do this,

console.log(app.contacts[0]);

I always get the first row only.

Can I use jquery.get() to get a row from the list, something like this below?

console.log(app.contacts.get({id:5}));

回答1:


You have to iterate and check the id or email property in the loop

var obj;

for (var i=0; i<arr.length; i++) {
    if ( arr[i].id == '40' ) {
        obj = arr[i];
        break;
    }
}

FIDDLE

Also, the posted array / objects are not valid



来源:https://stackoverflow.com/questions/20813223/jquery-how-to-get-a-row-of-json-data-from-the-json-data-list-by-id-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!