jquery: how to get a row of json data from the jso

2019-09-02 02:22发布

问题:

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