【问题标题】:How do i access nested array values in angular.js?如何访问 angular.js 中的嵌套数组值?
【发布时间】: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


    【解决方案1】:

    contributorDetails是一个json数组

    你可以这样访问它

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

    jsFiddle

    【讨论】:

      【解决方案2】:

      试试下面的方法

      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);
          }
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        contributorDetails 是对象元素中的一个数组。试试这样的

        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;
        

        【讨论】:

        • 是的,我在 for 循环中获得了 3 个对象元素,但在 for 循环之外我只获得了最后一个。
        • 如何在 for 循环之外获取 3 个元素?
        • 我不确定你的意思。根据您的代码,当您这样做时(键入 res.results)。您正在循环遍历 _id、contributorProductName、contributorProductId 等元素,尝试使用 res.results.contributorDetails 中的每个项目进行循环
        • form 外面,你必须知道索引... obj 是 res.result 基于你的代码,obj[index].name,email 或数量。
        【解决方案4】:

        尝试 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
        
        });
        

        【讨论】:

          猜你喜欢
          • 2018-08-16
          • 1970-01-01
          • 2019-10-31
          • 1970-01-01
          • 2022-05-08
          • 2019-01-22
          • 2020-05-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多