我有包含User
对象的Javascript数组。我从modelAttribute
创建了这个数组。如何读取Javascript数组中的对象
var userList = '${userList}'; // userList is a spring model attribute
userList
包含User
对象列表。我正在访问它作为
for(i=0;i<userList.length;i++)
{
if(searchKey == "" || userList[i].indexOf(searchKey) != -1)
{
$('#userTable').dataTable().fnAddData([
userList[i].firstName,
userList[i].lastName,
userList[i].institution,
userList[i].email]);
}
}
但我得到的值为undefined
。最初,我使用Ajax调用相同,它工作正常。
$.getJSON("lookup/users", {name:searchKey,userType:"requester"}, function(userList) {
// It works fine
for(i=0;i<userList.length;i++)
{
$('#userTable').dataTable().fnAddData([
userList[i].firstName,
userList[i].lastName,
userList[i].institution,
userList[i].email]);
}
});
我现在怎样访问它?
编辑:
的console.log( “用户列表:” +用户列表);给出
userList : [[email protected], [email protected], [email protected]]
尝试的console.log(用户列表),看看它实际上包含 – haynar
是的,在填充它之后,试着找出变量userList的确切结构。结构可能与您认为的不同。使用console.log或在分配后在萤火虫中放置一个断点并检查您的手表块),或者只需在萤火虫控制台中键入userList即可。 – techfoobar
@haynar:请检查我的编辑。 – xyz