我有一个对象,我在其中使用JSON Stringify来查看其内容,如下所示:
var testing = JSON.stringify($scope.test, null, 4);
当我console.log(testing)
时,对象看起来像这样:
{
"_id": "53e866a8a595b7041f9510c9",
"start": "2014-08-04T07:00:00.000Z",
"end": "2014-08-16T07:00:00.000Z",
"location": "Australia",
"name": "Joe's Surprise",
"__v": 1,
"array": [
{
"_id": "53ddc8c98ae4813c0420e189",
"provider": "local",
"name": "Test User",
"username": "testUser",
"email": "test@test.com",
"hashedPassword": "e5ri7OVhzNQMZpSqxnB3p2FyrpxskFE3yM8jHn5hfzZZvdd57YhhJrjFWJqBQhhyZz6y8UG68mr+rQ95admtfw==",
"salt": "PVEFtMfyJ/7TX9Do0cYMdQ==",
"__v": 2,
"attending": [
"53e866a8a595b7041f9510c9"
],
"role": "user"
},
]
}
但是,我想在变量测试的array
属性中打印出username属性,但我无法这样做。我试过像这样做一个for循环:
for(var i = 0; i < testing.array.length; i++){
console.log(testing.array[i].username);
}
但.length属性被视为undefined
。我还尝试过简单地执行console.log(testing._id)
以查看是否有效但返回undefined
。我不确定我做错了什么,有人可以帮忙吗?谢谢!
答案 0 :(得分:3)
var testing = JSON.stringify($scope.test, null, 4);
将$ scope.test转换为字符串(可能是因为您可以以人类可读格式查看)。字符串不包含任何数组或属性。你想要它的原始形式,而不是字符串。
你可能想要: for(var i = 0; i < $scope.test.array.length; i++){
console.log($scope.test.array[i].username);
}