简体   繁体   English

如何在angular.js中访问嵌套数组值?

[英]How do i access nested array values in angular.js?

I want to access the objects present in the array inside the another array, its like below. 我想访问另一个数组内部的数组中存在的对象,如下所示。

{
"_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" 
   } 
 ] 
}

Now I want to access individual objects present in the contributorDetails array but i am getting only last object in that array. 现在,我想访问contributorDetails数组中存在的单个对象,但是我只获得该数组中的最后一个对象。 I have tried like below in angular.js controller. 我已经尝试过以下在angular.js控制器中。

controllerFile.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');
        });     
}

Someone please help. 有人请帮忙。 输出量

contributorDetails is an array within your object elements. contributorDetails是对象元素内的数组。 Try something like this 试试这个

obj.contributorDetails[i].name, //i is the index if you know the index you want to access

//or get them all
for (var i = 0 ; i < obj.contributorDetails.length; i++)
{
  obj.contributorDetails[i].name
  obj.contributorDetails[i].email
  obj.contributorDetails[i].amount
} //in your case, obj = res.result;

contributorDetails is a json array contributorDetails是一个json数组

You can access it like this way 您可以这样访问

res.contributorDetails.forEach(function(item){
 document.write('<pre>'+item.name + ' -- '+ item.email+'</pre>'+'</br>')

})

jsFiddle jsFiddle

Try something like below 试试下面的东西

vm.result = [];
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.push(res.result[key); //Push into the array
        }

        console.log(vm.result);
    }

Hope this will help. 希望这会有所帮助。

Try angular.forEach (assuming your object name to be $scope.obj): 尝试angular.forEach(假设您的对象名称为$ scope.obj):

angular.forEach($scope.obj.contributorDetails, function(item){

    //each item is an individual object, to access individual values you can do this:
    //item.name
    //item.email
    //item.amount

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM