如何使用es6 map返回匹配的对象?

时间:2018-12-31 03:02:50

标签: javascript arrays ecmascript-6

尝试使用es6映射方法,因此在下面,我试图从与键匹配的cacheDetails函数中获取对象,但它还会返回一些未定义的值,以下代码中实现错误的任何帮助将不胜感激?

main.js

var uniqueCacheKeys = [{key:"14556459709"},{key:"1457099"}];
var rxInfos = [];
function getCahce(data){
  data.forEach(function(el){
    var rxInfo = cacheDetails(el.key);
    if(rxInfo){
      rxInfos.push(rxInfo);
    } 
  });
}
function cacheDetails(key){
  var mapObj;
   var rxInfos = [
      {
            "drugNdc": "10101",
            "rxNumber": "14556459709",
            "firstFillIndicator": "N",
            "sourceSystem": "TBS",
            "indexID": "RPT0ifQ",
            "ancilary": true
        },{
            "drugNdc": "10101",
            "rxNumber": "145709",
            "firstFillIndicator": "N",
            "sourceSystem": "TBS",
            "indexID": "RPT0ifQ",
            "ancilary": true
        }];
  mapObj = rxInfos.map(function (task, index, array) {
   if(task.rxNumber === key){
        return task; 
   }
});

  return mapObj;
}
getCahce(uniqueCacheKeys);

console.log(rxInfos);

输出

[[[object Object] {
  ancilary: true,
  drugNdc: "10101",
  firstFillIndicator: "N",
  indexID: "RPT0ifQ",
  rxNumber: "14556459709",
  sourceSystem: "TBS"
}, undefined], [undefined, undefined]]

具有上述数据的期望输出

[{
  ancilary: true,
  drugNdc: "10101",
  firstFillIndicator: "N",
  indexID: "RPT0ifQ",
  rxNumber: "14556459709",
  sourceSystem: "TBS"
}]

0 个答案:

没有答案