我添加了我想循环的代码和json。
我使用foreach循环循环了$scope.data
并推送到$scope.tempObj
,它的工作正常。但是,当我尝试循环$scope.data.policyDocumentContentCollection
并推送到$scope.tempObj.nested
时,其获取控制台错误“无法读取未定义的属性'push'”。我能知道如何将第二个foreach数据推送到$scope.tempObj
中的嵌套数组中。
$scope.data = [
{
"policyNo":"DBDP18S016696",
"renewalNo":"001",
"endorseNo":"001",
"productDesc":"TBD",
"policyDocumentContentCollection":[
{
"docType":"Credit Note1"
},
{
"docType":"Tax Invoice1"
}
]
},
{
"policyNo":"DBDP18S016697",
"renewalNo":"001",
"endorseNo":"001",
"productDesc":"TBD",
"policyDocumentContentCollection":[
{
"docType":"Credit Note1"
},
{
"docType":"Tax Invoice1"
}
]
},
{
"policyNo":"DBDP18S016698",
"renewalNo":"001",
"endorseNo":"001",
"productDesc":"TBD",
"policyDocumentContentCollection":[
{
"docType":"Credit Note1"
},
{
"docType":"Tax Invoice1"
}
]
}
]
$scope.tempObj = [];
angular.forEach($scope.data, function (data) {
$scope.tempObj.push({
'id': data.policyNo,
'renewalNo': data.renewalNo,
'endorsementNo': data.endorseNo,
'product': data.productDesc,
'insuredName': data.holderName,
'periodOfInsurance': data.fromDate + ' to ' + data.toDate,
'nested': []
});
angular.forEach($scope.data.policyDocumentContentCollection, function (docs) {
$scope.tempObj.nested.push({
docName: docs.docType
})
});
});
答案 0 :(得分:4)
您可以创建一个中间对象,以便将其推入当前元素内。现在您不能,因为您按下了不存在的$scope.tempObj.nested
,因为$scope.tempObj[index].nested
是一个数组,所以只有$scope.tempObj
存在。
angular.forEach($scope.data, function(data) {
const element = {
id: data.policyNo,
renewalNo: data.renewalNo,
endorsementNo: data.endorseNo,
product: data.productDesc,
insuredName: data.holderName,
periodOfInsurance: data.fromDate + " to " + data.toDate,
nested: []
};
angular.forEach(data.policyDocumentContentCollection, function(docs) {
element.nested.push({
docName: docs.docType
});
});
$scope.tempObj.push(element);
});
答案 1 :(得分:0)
$scope.tempObj.nested
不存在,因为$scope.tempObj
是对象数组。因此,您必须首先在index
数组的特定$scope.tempObj
处访问对象,然后将值压入$scope.tempObj[index].nested
数组。
$scope.data = [
{
"policyNo":"DBDP18S016696",
"renewalNo":"001",
"endorseNo":"001",
"productDesc":"TBD",
"policyDocumentContentCollection":[
{
"docType":"Credit Note1"
},
{
"docType":"Tax Invoice1"
}
]
},
{
"policyNo":"DBDP18S016697",
"renewalNo":"001",
"endorseNo":"001",
"productDesc":"TBD",
"policyDocumentContentCollection":[
{
"docType":"Credit Note1"
},
{
"docType":"Tax Invoice1"
}
]
},
{
"policyNo":"DBDP18S016698",
"renewalNo":"001",
"endorseNo":"001",
"productDesc":"TBD",
"policyDocumentContentCollection":[
{
"docType":"Credit Note1"
},
{
"docType":"Tax Invoice1"
}
]
}
]
$scope.tempObj = {};
angular.forEach($scope.data, function (data, index) {
$scope.tempObj = {
'id': data.policyNo,
'renewalNo': data.renewalNo,
'endorsementNo': data.endorseNo,
'product': data.productDesc,
'insuredName': data.holderName,
'periodOfInsurance': data.fromDate + ' to ' + data.toDate,
'nested': []
};
angular.forEach($scope.data.policyDocumentContentCollection, function (docs) {
$scope.tempObj[index].nested.push({
docName: docs.docType
})
});
});
j