面对打印arraylist的问题

时间:2018-02-27 05:47:12

标签: javascript angularjs

  

代码:

var cspaObj = {};
var associatedCspaList = responseData.data.associatedCspaList;
responseData.data.associatedCspaList = [];
angular.forEach(associatedCspaList, function (value, key) {
  cspaObj.clientId = value.slice(0, 4);
  cspaObj.systemId = value.slice(4, 8);
  cspaObj.principleId = value.slice(8, 12);
  cspaObj.agentId = value.slice(12, 16);
  responseData.data.associatedCspaList.push(cspaObj);
});
  

以下是associatedCspaList

 "associatedCspaList": [
  "1000100010001000",
  "2000200020002000",
  "3000300030003000",
  "9999100020003000",
  "9999999999999999"
]
  

需要打印如下:

1000 1000 1000 1000
2000 2000 2000 2000
3000 3000 3000 3000
9999 1000 2000 3000
9999 9999 9999 9999

但上面的代码只打印了最后一个9999 9999 9999 9999.它不会打印剩余的值,所以请帮助我。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

我认为你的cspaObj变量应该在循环中。



var associatedCspaList = [
  "1000100010001001",
  "2000200020002000",
  "3000300030003000",
  "9999100020003000",
  "9999999999999999"
];

var associatedCspaListResponse = [];

angular.forEach(associatedCspaList, function (value, key) {
  var cspaObj = {};
  var output = "";
  cspaObj.clientId = value.slice(0, 4);
  cspaObj.systemId = value.slice(4, 8);
  cspaObj.principleId = value.slice(8, 12);
  cspaObj.agentId = value.slice(12, 16);
  
  // expected output
  console.log(cspaObj.clientId + " " + cspaObj.systemId + " " + cspaObj.principleId + " " + cspaObj.agentId );
  
  //response
  
  associatedCspaListResponse.push(cspaObj);
});
console.log("response");
console.log(associatedCspaListResponse);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
&#13;
&#13;
&#13;