简体   繁体   English

根据属性的排序顺序重新映射 Object 数组

[英]Re-Map an array of Object based on sort order of a property

Let's see I have a data in following structure让我们看看我有以下结构的数据

[
    {
        "Id": "xyz7",
        "CurrentRow": 0,
        "ReportTime": "2022-07-18T09:00:00+00:00",
        "ExitTime": null,
        "DateField": "2022-07-18"
    },
    {
        "Id": "xyz8",
        "CurrentRow": 1,
        "ReportTime": "2022-07-18T08:00:00+00:00",
        "ExitTime": null,
        "DateField": "2022-07-18"
    },
    {
        "Id": "wxyz0",
        "CurrentRow": 0,
        "ReportTime": "2022-07-19T00:00:00+00:00",
        "ExitTime": null,
        "DateField": "2022-07-19"
    },
    {
        "Id": "wxyz1",
        "CurrentRow": 1,
        "ReportTime": "2022-07-19T00:00:00+00:00",
        "ExitTime": null
        "DateField": "2022-07-19"
    }
]

If I have to say sort the structure based on ReportTime of Date: 2022-07-18, that will change the CurrentRow of entries for DateField 2022-07-18 as 0 to 1 (as it will now belong to 1st Index) and for 2nd entry 1 - 0.如果我不得不说根据 ReportTime of Date: 2022-07-18 对结构进行排序,这会将 DateField 2022-07-18 的条目的 CurrentRow 更改为 0 到 1(因为它现在属于第一个索引)和第二个条目 1 - 0。

In addition, the CurrentRow of other entries (for other date shall also be adapted if they were same as that of day being sorted.)另外,其他条目的CurrentRow(对于其他日期,如果与被排序的日期相同,也要进行调整。)

In order to achieve this my implementation goes like,为了实现这一点,我的实现是这样的,

I convert the structure to a two dimensional array based on CurrentRow.我将结构转换为基于 CurrentRow 的二维数组。

Index in dimension 1, represents the CurrentRow.维度 1 中的索引,表示 CurrentRow。 The element of array will be an array of specific data entry like [entry_for_date_18,entry_for_date_19] (Kind of spread sheet with date as columns and CurrentRow as rows.数组的元素将是一个特定数据条目的数组,例如[entry_for_date_18,entry_for_date_19] (一种以日期为列,以 CurrentRow 为行的电子表格。

And then in order to sort, I pick all the entries for a particular date, sort it, and collect it with the original CurrentRow.然后为了进行排序,我选择了特定日期的所有条目,对其进行排序,然后将其与原始 CurrentRow 一起收集。 (Pass 1). (通过 1)。

Then I go and update the CurrentRow of original array, using the index (pass 2).然后我 go 并使用索引(通过 2)更新原始数组的 CurrentRow。

eg pseudo code:例如伪代码:

 for(let i=0;i<sortedDayArray.length;i++){
   findByInOriginalArray(sortedDayArray[i].CurrentRow).updateCurrentRowTo(i)
  }

Was wondering if there is a better or more efficient way to do that, using map?想知道是否有更好或更有效的方法来做到这一点,使用 map?

This is how I got your question: you want to sort your array based on ReportTime , then re-arrange CurrentRow based on its position in DateField and this is the data you are expecting:这就是我得到您的问题的方式:您想根据ReportTime对数组进行排序,然后根据DateField中的 position 重新排列CurrentRow ,这是您期望的数据:

[
  {
    Id: 'xyz8',
    CurrentRow: 0,
    ReportTime: '2022-07-18T08:00:00+00:00',
    ExitTime: null,
    DateField: '2022-07-18'
  },
  {
    Id: 'xyz7',
    CurrentRow: 1,
    ReportTime: '2022-07-18T09:00:00+00:00',
    ExitTime: null,
    DateField: '2022-07-18'
  },
  {
    Id: 'wxyz0',
    CurrentRow: 0,
    ReportTime: '2022-07-19T00:00:00+00:00',
    ExitTime: null,
    DateField: '2022-07-19'
  },
  {
    Id: 'wxyz1',
    CurrentRow: 1,
    ReportTime: '2022-07-19T00:00:00+00:00',
    ExitTime: null,
    DateField: '2022-07-19'
  }
]

this is the code I came up with:这是我想出的代码:

var tempRow = 0;
var tempDate = ''

YOUR_ARRAY
    .sort((a, b) => (a.ReportTime > b.ReportTime) ? 1 : ((b.ReportTime > a.ReportTime) ? -1 : 0))
    .forEach((row, i) => {
        if (row.DateField != tempDate) {
            tempDate = row.DateField
            tempRow = 0
        }
        row.CurrentRow = tempRow
        tempRow++
    })

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

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