提问者:小点点

JavaScript lodash数据转换


这是我的代码:它可以在在线lodash测试器中运行。 目前,我通过检查匹配的recall_ids,然后循环遍历数组以附加voq_count属性来获取voq_count。

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;
});

电流输出:

[
   {
      "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
   }
]

我试图循环查找匹配的recall_ids,并构造一个voqs数组,如下所示:

预期产出:

[
   {
      "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个答案

匿名用户

您可以使用mapfilter来选择数据:

null

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))
  return {...item, voqs_count:voqs.length,voqs}
});

console.log(result);