Javascript 如何访问服务器阵列响应中的对象字段?
我发出了一个Javascript 如何访问服务器阵列响应中的对象字段?,javascript,arrays,angularjs,parse-platform,Javascript,Arrays,Angularjs,Parse Platform,我发出了一个parse.comget请求,返回的数据存储在: $scope.tastes = data.results { "createdAt": "2016-03-16T07:39:15.745Z", "objectId": "Cmg8GdOv2Z", "updatedAt": "2016-03-16T07:39:15.745Z", "user": { "__type": "Pointer",
parse.com
get请求,返回的数据存储在:
$scope.tastes = data.results
{
"createdAt": "2016-03-16T07:39:15.745Z",
"objectId": "Cmg8GdOv2Z",
"updatedAt": "2016-03-16T07:39:15.745Z",
"user": {
"__type": "Pointer",
"className": "_User",
"objectId": "vYOsndWlto"
},
"userTastes": [
{
"actualite": {
"checked": true
},
"economie": {
"checked": true
},
"entrepreneuriat": {
"checked": false
}
}
]
}
嗯,我想得到一个数组
我试过了
.success(function (data, status) {
$scope.tastes = data.results.userTastes;
console.log($scope.tastes);
})
然而,没有任何回报。我想我错过了什么
我的问题:如何在$scope.tastes中获取userTastes?结果[0]。userTastes工作正常谢谢强> 如果有人有一个关于JS中数组和对象的教程链接或很好的课程,因为我对此有点困惑
祝你有愉快的一天 我写了一个单独的答案,因为我认为这需要进一步的解释,而不仅仅是解决你的问题 您在问题中只提供了一个对象响应,但显然您正在从服务器获得一个数组响应,而您可以直接访问对象字段,在数组对象上,您需要首先访问位置,例如:
$scope.objectResponse = {"foo":"bar"};
console.log($scope.objectResponse.foo); // Will print "bar"
相比之下,阵列响应:
$scope.arrayResponse = [{"foo":"bar"}];
console.log($scope.arrayResponse[0].foo); // Will print "bar"
只需确保您正在从服务器获得所需的响应。您是否尝试过
此.proxy(function)
或将上下文绑定到该函数?data.results[0].userTastes是否有效?,您是否尝试过控制台日志记录以查看数据是否返回任何内容?如果data.userTastes
,情况如何?如果没有基本示例,很难说发生了什么。你能提供一把小提琴吗@Jry9972您只能与50+代表发表评论…这是我现在得到的。