繁体   English   中英

如何将数据映射到具有特定字段和值的新数组,并根据日期范围每天添加一个对象

[英]How to map data to a new array with specific fields and values and add a single object per day based on a range of dates

我很难找到将对象数组映射到需要由特定字段和值定义对象的新数组的方法,而且我还需要在一天内添加对象,将进一步解释详细信息,我无法使用由于我的项目中有任何类型的代码样式限制,我的 for 循环

我需要映射到新数组的数据

[
    {
      id: 'C12-TBX4',
      studyId: 'TBX4',
      siteId: 'USA-1',
      statusType: 'INCOMPLETE',
      statusFrom: '2020-12-01',
      statusTo: '2020-12-05'
    },
    {
      id: 'C13-TBX4',
      studyId: 'TBX4',
      siteId: 'USA-1',
      statusType: 'INCOMPLETE',
      statusFrom: '2020-12-03',
      statusTo: '2020-12-07'
    },
    {
      id: 'C14-TBX4',
      studyId: 'TBX4',
      siteId: 'USA-1',
      statusType: 'INCOMPLETE',
      statusFrom: '2020-12-05',
      statusTo: '2020-12-08'
    },
    {
      id: 'C15-TBX4',
      studyId: 'TBX4',
      siteId: null,
      statusType: 'REJECTED',
      statusFrom: '2020-12-05',
      statusTo: '2020-12-08'
    },
    {
      id: 'C16-TBX4',
      studyId: 'TBX4',
      siteId: null,
      statusType: 'REJECTED',
      statusFrom: '2020-12-05',
      statusTo: '2020-12-09'
    },
    {
      id: 'C17-TBX4',
      studyId: 'TBX4',
      siteId: 'USA-1',
      statusType: 'DROPOUT',
      eligible: true,
      statusFrom: '2020-12-05',
      statusTo: '2020-12-09'
    },
    {
      id: 'C17-TBX4',
      studyId: 'TBX4',
      siteId: 'USA-1',
      statusType: 'DROPOUT',
      eligible: false,
      statusFrom: '2020-12-05',
      statusTo: '2020-12-10'
    }
  ]

上述数组需要使用以下日期进行比较和重新映射

 [
    2020-12-01T00:00:00.000Z,
    2020-12-02T00:00:00.000Z,
    2020-12-03T00:00:00.000Z,
    2020-12-04T00:00:00.000Z,
    2020-12-05T00:00:00.000Z,
    2020-12-06T00:00:00.000Z,
    2020-12-07T00:00:00.000Z,
    2020-12-08T00:00:00.000Z,
    2020-12-09T00:00:00.000Z
  ]

日期在数据对象的最小日期到最大日期的范围内。

数据对象包含一个区间作为statusFromstatusTo 我需要有一个新的对象数组,我们将从日期对象中获得一天。

该数组还将包括一个名为total的新字段,它是同一天具有相同statusType的单个研究中的id总数。

举一个我需要的结果的例子

[
    // INCOMPLETE
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-01",
        "statusType": "INCOMPLETE",
        "total": 1 // Only "id": "C12-TBX4",
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-02",
        "statusType": "INCOMPLETE",
        "total": 1 // Only "id": "C12-TBX4",
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-03",
        "statusType": "INCOMPLETE",
        "total": 2 // we have C13-TBX4 + C12-TBX4, dates are overlapping
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-03",
        "statusType": "INCOMPLETE",
        "total": 2 // we have C13-TBX4 + C12-TBX4, dates are overlapping
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-04",
        "statusType": "INCOMPLETE",
        "total": 2 // we have C13-TBX4 + C12-TBX4, dates are overlapping
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-05", // we include only status from and exclude status to
        "statusType": "INCOMPLETE",
        "total": 2 // we have C13-TBX4 + C14-TBX4, dates are overlapping -- C12-TBX4 is excluded
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-06",
        "statusType": "INCOMPLETE",
        "total": 2 // we have C13-TBX4 + C14-TBX4, dates are overlapping
    },
    {
        "studyId": "TBX4",
        "siteId": "USA-1",
        "day": "2020-12-07",
        "statusType": "INCOMPLETE",
        "total": 1 // we have C14-TBX4
    },
]

以上只是statusType: INCOMPLETE的示例,但其他状态需要执行相同的逻辑。

如您所见,目标是根据日期范围内的单个日期映射一个新数组,并将当天处于该状态的 id 总数加上一天。

我不包含任何片段,因为老实说不知道从哪里开始以及如何做

如果我理解正确,我们会得到包含一系列日期的研究,并且会得到一份研究范围包含的特定日期的列表。 我们希望生成研究对象以指示每个对象包含的特定日期,并根据匹配的日期和类型进行一些汇总。

 const data = [ { id: "C12-TBX4", studyId: "TBX4", siteId: "USA-1", statusType: "INCOMPLETE", statusFrom: "2020-12-01", statusTo: "2020-12-05" }, { id: "C13-TBX4", studyId: "TBX4", siteId: "USA-1", statusType: "INCOMPLETE", statusFrom: "2020-12-03", statusTo: "2020-12-07" }, { id: "C14-TBX4", studyId: "TBX4", siteId: "USA-1", statusType: "INCOMPLETE", statusFrom: "2020-12-05", statusTo: "2020-12-08" }, { id: "C16-TBX4", studyId: "TBX4", siteId: null, statusType: "REJECTED", statusFrom: "2020-12-05", statusTo: "2020-12-09" }, { id: "C17-TBX4", studyId: "TBX4", siteId: null, statusType: "REJECTED", statusFrom: "2020-12-05", statusTo: "2020-12-09" }, { id: "C18-TBX4", studyId: "TBX4", siteId: "USA-1", statusType: "DROPOUT", eligible: true, statusFrom: "2020-12-05", statusTo: "2020-12-09" }, { id: "C19-TBX4", studyId: "TBX4", siteId: "USA-1", statusType: "DROPOUT", eligible: false, statusFrom: "2020-12-05", statusTo: "2020-12-10" } ]; const rangeOfDates = [ new Date("2020-12-01T00:00:00.000Z"), new Date("2020-12-02T00:00:00.000Z"), new Date("2020-12-03T00:00:00.000Z"), new Date("2020-12-04T00:00:00.000Z"), new Date("2020-12-05T00:00:00.000Z"), new Date("2020-12-06T00:00:00.000Z"), new Date("2020-12-07T00:00:00.000Z"), new Date("2020-12-08T00:00:00.000Z"), new Date("2020-12-09T00:00:00.000Z") ]; // prepare input objects for search by date let studies = data.map((s) => { let r = Object.assign({}, s); // getTime() gives scalar ms since the epoch, for simpler comparisons r.statusFrom = new Date(r.statusFrom).getTime(); r.statusTo = r.statusTo ? new Date(r.statusTo).getTime() : 8640000000000000; return r; }); // same for the times let times = rangeOfDates.map((d) => { let time = d.getTime(); let day = d.toISOString().split("T")[0]; // the day part of the string return { time, day }; }); let resultIndex = {}; times.forEach((t) => { // get the matching studies, recall that t is an epoch time and a day string let matchingStudies = studies.filter(s => { return s.statusFrom <= t.time && t.time < s.statusTo; }); let idIndex = {}; // particularize the matching studies with the matching day, and requiring a unique studyId-day matchingStudies.forEach(ms => { let r = { day: t.day, studyId: ms.studyId, siteId: ms.siteId, statusType: ms.statusType, total: 0 }; // require uniqueness of studyId-day, second input prevails let key = `${r.day}${r.studyId}`; idIndex[key] = r; }); matchingStudies = Object.values(idIndex); // summarize totals by statusType-day matchingStudies.forEach(ms => { let key = `${ms.day}${ms.statusType}`; if (!resultIndex[key]) resultIndex[key] = ms; resultIndex[key].total++; }) }); let result = Object.values(resultIndex); console.log(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM