【发布时间】:2016-06-01 05:15:40
【问题描述】:
我想访问另一个数组内的数组中存在的对象,如下所示。
{
"_id": ObjectId("574d2aa42ec356541dc7034e"),
"contributedProductName": "ITOS365 Radha Krishna Brass Statue Hindu God Sculpture Religious Gifts Item, 5.5 Inches",
"contributedProductId": "57459ef82b01e7802ca9294b",
"receiverId": "570dec75cf30bf4c09679deb",
"contributorDetails": [
{
"name": "Hemanth",
"email": "hemanth@gmail.com",
"amount": "500"
},
{
"name": "Kevin",
"email": "kevinguru888@gmail.com",
"amount": "300"
},
{
"name": "vinay",
"email": "vinay@gmail.com",
"amount": "149"
}
]
}
现在我想访问contributorDetails 数组中存在的单个对象,但我只得到该数组中的最后一个对象。我在 angular.js 控制器中尝试过如下所示。
controllerFile.js
function ManageContributorController($http, $location, $rootScope, $localStorage)
{
var vm = this;
vm.uid = $localStorage._id;
//console.log(vm.uid);
$http({
url: 'http://192.168.2.8:7200/api/manage-contributor',
method: 'POST',
data: {userId:vm.uid}
}).success(function(res) {
for(var key in res.result){
for(var anotherKey in res.result[key].contributorDetails){
var cName = res.result[key].contributorDetails[anotherKey].name;
var cEmail = res.result[key].contributorDetails[anotherKey].email;
var cAmnt = res.result[key].contributorDetails[anotherKey].amount;
}
res.result[key]['contributorName']=cName;
res.result[key]['contributorEmail']=cEmail;
res.result[key]['contributedAmount']=cAmnt;
}
vm.result = res.result;
console.log(vm.result);
}, function(error) {
console.log(error);
alert('here');
});
}
【问题讨论】:
标签: javascript arrays angularjs mongodb