JavaScript lodash data transformation

问题: Here's my code: It could be run in an online lodash tester. Currently, I am getting voq_count by checking the matching recall_ids and then looping through the array to atta...

问题:

Here's my code: It could be run in an online lodash tester. Currently, I am getting voq_count by checking the matching recall_ids and then looping through the array to attach voq_count property.

let array = [{
        "recall_id": 527,
        "type_cd": "E",
        "odi_id": "70E001"
    },
    {
        "recall_id": 528,
        "type_cd": "T",
        "odi_id": "70T002"
    },
    {
        "recall_id": 529,
        "type_cd": "T",
        "odi_id": "70T003"
    }
];

let dataItem = {
    "results": [
        {
            "p_hazards": [{
                    "entity_record_id": 529
                },
                {
                    "entity_record_id": 528
                }
            ],
            "received_dt": "2005-08-24T00:00:00.000Z",
            "odi_id": "55555825",
            "complaint_id": 55555833,
            "number_injured": 4,
            "number_deaths": 1
        },
        {
            "p_hazards": [{
                    "entity_record_id": 547
                },
                {
                    "entity_record_id": 545
                },
                {
                    "entity_record_id": 546
                }
            ],
            "received_dt": "2005-08-24T00:00:00.000Z",
            "odi_id": "55555636",
            "complaint_id": 55555646,
            "number_injured": 0,
            "number_deaths": 0
        },
        {
            "p_hazards": [{
                "entity_record_id": 528
            }],
            "received_dt": "2005-08-29T00:00:00.000Z",
            "odi_id": "1147792",
            "complaint_id": 1147792,
            "number_injured": 0,
            "number_deaths": 0
        },
    ]
};

const RIds = [];

_.map(dataItem.results, (ph) => {
    _.map(ph.p_hazards, (id) => {
        RIds.push(id);
    });
});


const getCount = _.countBy(
    RIds,
    'entity_record_id'
);

_.map(array, (r) => {
    const voqCount = getCount[r.recall_id.toString()];
    r.voq_count = voqCount || 0;
    return r;
});

Current output:

[
   {
      "recall_id": 527,
      "type_cd": "E",
      "odi_id": "70E001",
      "voq_count": 0
   },
   {
      "recall_id": 528,
      "type_cd": "T",
      "odi_id": "70T002",
      "voq_count": 2
   },
   {
      "recall_id": 529,
      "type_cd": "T",
      "odi_id": "70T003",
      "voq_count": 1
   }
]

I am trying to loop through for the matching recall_ids and construct a voqs array which would look like:

Expected Output:

[
   {
      "recall_id": 527,
      "type_cd": "E",
      "odi_id": "70E001",
      "voq_count": 0,
"voqs": []
   },
   {
      "recall_id": 528,
      "type_cd": "T",
      "odi_id": "70T002",
      "voq_count": 2,
"voqs": [{
            "received_dt": "2005-08-24T00:00:00.000Z",
            "odi_id": "55555825",
            "complaint_id": 55555833,
            "number_injured": 4,
            "number_deaths": 1
}, {
            "received_dt": "2005-08-29T00:00:00.000Z",
            "odi_id": "1147792",
            "complaint_id": 1147792,
            "number_injured": 0,
            "number_deaths": 0
}]
   },
   {
      "recall_id": 529,
      "type_cd": "T",
      "odi_id": "70T003",
      "voq_count": 1,
"voqs": [{
            "received_dt": "2005-08-24T00:00:00.000Z",
            "odi_id": "55555825",
            "complaint_id": 55555833,
            "number_injured": 4,
            "number_deaths": 1
}
]
   }
]

回答1:

You can make use of map and for selecting the data filter will work:

var dataItem = { "results": [ { "p_hazards": [{ "entity_record_id": 529 }, { "entity_record_id": 528 } ], "received_dt": "2005-08-24T00:00:00.000Z", "odi_id": "55555825", "complaint_id": 55555833, "number_injured": 4, "number_deaths": 1 }, { "p_hazards": [{ "entity_record_id": 547 }, { "entity_record_id": 545 }, { "entity_record_id": 546 } ], "received_dt": "2005-08-24T00:00:00.000Z", "odi_id": "55555636", "complaint_id": 55555646, "number_injured": 0, "number_deaths": 0 }, { "p_hazards": [{ "entity_record_id": 528 }], "received_dt": "2005-08-29T00:00:00.000Z", "odi_id": "1147792", "complaint_id": 1147792, "number_injured": 0, "number_deaths": 0 }, ]};
var array = [{ "recall_id": 527, "type_cd": "E", "odi_id": "70E001" }, { "recall_id": 528, "type_cd": "T", "odi_id": "70T002" }, { "recall_id": 529, "type_cd": "T", "odi_id": "70T003" }];

var result = array.map(item=>{
    voqs = dataItem.results.filter(k=>k.p_hazards.some(l=>l.entity_record_id==item.recall_id)).map(({p_hazards, ...rest})=>({...rest}));
  return {...item, voqs_count:voqs.length,voqs}
});

console.log(result);

  • 发表于 2020-06-27 22:56
  • 阅读 ( 303 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除