{
"ordersList": [{
"ordersDto": {
"testMast": {
"testId": 9,
"testName": "HIV"
},
"sample": {
"sampleId": 9050
}
}
}, {
"ordersDto": {
"testMast": {
"testId": 1,
"testName": "VDRL"
},
"sample": null
}
}, {
"ordersDto": {
"testMast": {
"testId": 11,
"testName": "HIV1&2"
},
"sample": null
}
}, {
"ordersDto": {
"testMast": {
"testId": 3,
"testName": "HCB"
},
"sample": {
"sampleId": 9050
}
}
}, {
"ordersDto": {
"testMast": {
"testId": 10,
"testName": "HIV 1&2 Test1"
},
"sample": {
"sampleId": 9051
}
}
}]
}
需要在数组中获取非重复的样本ID
sample Id = 9050, 9051.
此外,我需要为每个样本ID获取测试名称,重复样本ID我需要将testname添加到同一个数组中。
testName = HIV, HCB // for 9050
testName = HIV 1&2 Test1" // for 9051
如何在一次迭代中填充这两个数组。还要根据样本Id值获取testName。 是否存在以角度存储的任何键值对?
尝试使用下面的代码,但没有按预期工作。
if (vm.ordersList== 1) { // incase of one sample Id
res.push(vm.ordersList[0].ordersDto.testMast.testName.slice(0, 4));
sampleId.push(angular.copy(vm.ordersList[0].ordersDto.sample.sampleId));
sampleRcvdDate.push(angular.copy(vm.ordersList[0].ordersDto.sample.sampleRcvdOn));
} else {
angular.forEach(vm.ordersList, function(item) { // multiple sample id's
if (item.sample != null) {
if(item.sample.sampleId != null && sampleId.indexOf(item.sample.sampleId) == -1 ){
res.push(item.testMast.testName.slice(0, 4));
sampleId.push(angular.copy(item.sample.sampleId));
sampleRcvdDate.push(angular.copy(item.sample.sampleRcvdOn));
}
}
})
}
答案 0 :(得分:2)
sampleIdTestNameMap将sampleId作为键,testName列表作为值。
var sampleIdTestNameMap = {};
angular.forEach(vm.ordersList, function(item) {
if (item.ordersDto.sample != null && item.ordersDto.sample.sampleId != null) {
if (sampleId.indexOf(item.ordersDto.sample.sampleId) == -1) {
sampleId.push(angular.copy(item.ordersDto.sample.sampleId));
sampleIdTestNameMap[item.ordersDto.sample.sampleId] = [item.ordersDto.testMast.testName];
} else {
var tempList = [];
tempList = sampleIdTestNameMap[item.ordersDto.sample.sampleId];
tempList.push(item.ordersDto.testMast.testName);
sampleIdTestNameMap[item.ordersDto.sample.sampleId] = tempList;
}
}
});