如何从一个对象数组中获取属性?

时间:2015-06-05 15:18:34

标签: javascript arrays json angularjs

我在json响应中有对象数组我想从riskAssessmentKey数组中获取includeAssessmentList并通过Post Api将该键传递给后端,我无法从数组中获取属性。我怎样才能完成这项任务?

到目前为止尝试过代码......

main.js

$scope.deleteAssess = function(){
          var assessmentKey = [];
          assessmentKey = $scope.rcsaCycleDTO.includeAssessmentList;
          for (var i=0; i<assessmentKey.length; i++) {
              var riskAssessmentKeys = assessmentKey[i];
              console.log(riskAssessmentKeys);
          }
          RcsaFactory.assessmentDetails(assessmentKey,$scope.rcsaCycleDTO.statusLookUpCode).then(function(){
            setConfirmationWinButtons(true, true, true);
            $scope.confirmationWin.open().center();
            $scope.confirmationWin.close();
          });
        };

JSON.js

[{includeAssessmentList: 
0: {cycleName: "Test startassment1", assessmentName: "Algeria_Angola", scopeType: null,…}
assessmentEndDate: "06/06/2015"
assessmentName: "Algeria_Angola"
assessmentRoleTypeDTOs: []
assessmentStartDate: "06/05/2015"
assessmentStatus: "Draft"
assessmentStatusLookUpCode: "RA_ASES_DRAFT"
availableRolesKey: []
beginTransactionTime: 1433422729000
createWorkerKey: -1066
createWorkerText: "NBKTHMC"
createdTimestamp: 1433495095000
currentFlag: 1
cycleEndDate: "06/06/2015"
cycleName: "Test startassment1"
cycleStartDate: "06/05/2015"
cycleType: "RA_RCSA"
dueDate: "06/26/2015"
endTransactionTime: 1433495095000
erhFlag: null
erhList: null
filteredErhList: null
geoLocationLst: null
legalEntity: null
modifiedTimestamp: 1433422729000
modifiedTimestampString: "06/04/2015"
modifiedUserText: "NBKTHMC"
modifyWorkerKey: -1066
rcsaOwnerWorkerKey: null
rcsaPreparerWorkerKey: null
responseMesg: null
riskAssessmentCycleKey: null
riskAssessmentKey: 87
riskAssessmentRoleTypeKey: null
scopeType: null
selectedRolesKey: []
sourceFeed: 1
statusLookUpCode: "RS_ACTIVE"
}]

1 个答案:

答案 0 :(得分:1)

for循环中,您只读取数组项而不是必需属性riskAssessmentKey。试试这个。

for (var i=0; i<assessmentKey.length; i++) {
   var riskAssessmentKeys = assessmentKey[i].riskAssessmentKey;
   console.log(riskAssessmentKeys);
}

如果includeAssessmentList始终只包含一个项目,那么您不需要for循环就可以试试。

if ($scope.rcsaCycleDTO.includeAssessmentList && $scope.rcsaCycleDTO.includeAssessmentList.length) {
  var riskAssessmentKeys = $scope.rcsaCycleDTO.includeAssessmentList[0].riskAssessmentKey;
}

如果你必须从数组中读取所有ristAssetKeyKeys,那么试试这个。

   var riskAssessmentKeys = [];
   for (var i=0; i<assessmentKey.length; i++) {
      riskAssessmentKeys.push(assessmentKey[i].riskAssessmentKey);
   }