2016-09-26 108 views
1
{ 
    "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 = 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

您可以将密钥和值对存储在地图中 – Hmahwish

+0

我可以在插入后更新相同的地图值。 – user630209

+0

是的你可以有一个sampleId作为键和一个testName的数组 – Hmahwish

回答

2

sampleIdTestNameMap将sampleId作为key和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; 
     } 
    } 
});