我用listjs (listjs.com)列出了一个列表
现在我使用函数get(),这个函数将给我返回:
itemsInList = [
{ id: 1, name: "Jonny" }
, { id: 2, name "Gustaf" }
];
listObj.get("id", 2); -> return { id: 2, name: "Gustaf" }现在,我只想获得带有纯javascript的名称id。
function getUserName(uid) {
var itemValues = hackerList.get("id", uid)[0];
console.log("The name of the user is: " + itemValues.name);
// or use alert..
//alert("The name of the user is: " + itemValues.name);
}抱歉不是混凝土..。我希望从返回值:
{ uid: 2,名称:"Marc“. }
致itemValues.name -> Marc
Udate
忘记使用uid函数("id")。当我使用value()时,它将工作。谢谢你的回答。
更新Fiddle 小提琴
发布于 2013-12-21 22:23:33
您的id字段名为uid,要获取对象的值,需要对结果调用.values,因此:
function getUserName(uid) {
var itemValues = hackerList.get("uid", uid)[0].values();
console.log("The name of the user is: " + itemValues.name);
}发布于 2013-12-21 22:21:35
可以使用数组筛选方法(IE9+):
// Pure javascript list
var hackerList = [
{ uid: 1, name: 'John' },
{ uid: 2, name: 'Marc' }
];
function getUserName(uid) {
var hackers = hackerList.filter(function(hacker) {
return hacker.uid === uid;
});
if(hackers.length > 0) {
console.log("The name of the user is: " + hackers[0].name);
}
}https://stackoverflow.com/questions/20724042
复制相似问题