문제

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}));
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top