摸索数组中的相似记录一个键?

Posted

技术标签:

【中文标题】摸索数组中的相似记录一个键?【英文标题】:Groping simillar records in an array a key? 【发布时间】:2020-12-10 06:06:06 【问题描述】:

我下面有一个这样的数组

[
  
    "Date": "2020-07",
    "data": [
      
        "id": "35ebd073-600c-4be4-a750-41c4be5ed24a",
        "Date": "2020-07-03T00:00:00.000Z",
        "transactionId": "13",
        "transactionType": "Payment",
        "amount": 1500
      
    ]
  ,
  
    "Date": "2020-07",
    "data": [
      
        "id": "4e126519-e27b-4e82-bb81-689c7dc63c9b",
        "Date": "2020-07-02T00:00:00.000Z",
        "transactionId": "4",
        "transactionType": "Payment",
        "amount": 1000
      
    ]
  ,
  
    "Date": "2020-06",
    "data": [
      
        "id": "646d6497-9dea-4f27-896e-a45d97a252ea",
        "Date": "2020-06-04T00:00:00.000Z",
        "transactionId": "14",
        "transactionType": "Payment",
        "amount": 1500
      
    ]
  ,
  
    "Date": "2020-06",
    "data": [
      
        "id": "cf44e27f-2111-462d-b3bd-420a193745b8",
        "Date": "2020-06-02T00:00:00.000Z",
        "transactionId": "5",
        "transactionType": "Payment",
        "amount": 1000
      
    ]
  
]

这里有键Date,同一个日期键有两个值。现在我想如果日期相同,那么数据数组记录应该合并。

所以我希望输出为

[
  
    "Date": "2020-07",
    "data": [
      
        "id": "35ebd073-600c-4be4-a750-41c4be5ed24a",
        "Date": "2020-07-03T00:00:00.000Z",
        "transactionId": "13",
        "transactionType": "Payment",
        "amount": 1500
      ,
      
        "id": "4e126519-e27b-4e82-bb81-689c7dc63c9b",
        "Date": "2020-07-02T00:00:00.000Z",
        "transactionId": "4",
        "transactionType": "Payment",
        "amount": 1000
      
    ]
  ,
  
    "Date": "2020-06",
    "data": [
      
        "id": "646d6497-9dea-4f27-896e-a45d97a252ea",
        "Date": "2020-06-04T00:00:00.000Z",
        "transactionId": "14",
        "transactionType": "Payment",
        "amount": 1500
      ,
       
        "id": "cf44e27f-2111-462d-b3bd-420a193745b8",
        "Date": "2020-06-02T00:00:00.000Z",
        "transactionId": "5",
        "transactionType": "Payment",
        "amount": 1000
      
    ]
  
]

请告诉我最好的优化方式吗?

【问题讨论】:

这能回答你的问题吗? Group object values by date 【参考方案1】:

您可以使用dictionary 并轻松检查密钥是否已存在,然后追加新数据,否则您将添加日期作为新密钥。

var dict = ;
if (!("xxxx-xx" in dict)) //if the key is not the dict

    dict["xxxx-xx"] = [       //we are storing an array because we want to add other later
         id: "....",
         transactionId: "..."
         //etc...
    ]


else                            //if the key already exists, we push new data to the array
    dict["xxxx-xx"].push(       //you can use push because is an array of objects
         id: "....",
         transactionId: "..."
         //etc...
    )

我希望它会有所帮助。 array 不太方便检查键是否已经存在避免重复(默认情况下使用dict/object)。

【讨论】:

请根据那个检查我想要的预期输出。 日期字段是多余的,当然如果你真的需要那个输出,这不是一个选择,但更适合你想做的事情。而不是“日期和数据”的对象,您只有一个标识他的数据的键 如果输出无法更改,这是一个有效的答案***.com/a/40775082/4757993【参考方案2】:

arr = 你的数据集

obj = 这将是你的输出

arr.forEach((elem) => 
    if(obj[elem.Date]) 
       obj[elem.Date].data.push(elem.data);
     else 
       obj[elem.Date] = 
       data: [elem.data]
    
  
);

【讨论】:

请根据此检查我想要的预期输出。【参考方案3】:

首先,您必须按属性Date 对该对象数组进行分组。并迭代按[Date, groupsByDate] 的键值对分组,然后从每个组中获取数据

const res = _.chain(data)
  .groupBy("Date")
  .toPairs()
  .map(([key, value]) => (
    Date: key,
    data: _.flatMap(value, "data"),
  ))
  .value()

完整代码

const data = [
  
    Date: "2020-07",
    data: [
      
        id: "35ebd073-600c-4be4-a750-41c4be5ed24a",
        Date: "2020-07-03T00:00:00.000Z",
        transactionId: "13",
        transactionType: "Payment",
        amount: 1500,
      ,
    ],
  ,
  
    Date: "2020-07",
    data: [
      
        id: "4e126519-e27b-4e82-bb81-689c7dc63c9b",
        Date: "2020-07-02T00:00:00.000Z",
        transactionId: "4",
        transactionType: "Payment",
        amount: 1000,
      ,
    ],
  ,
  
    Date: "2020-06",
    data: [
      
        id: "646d6497-9dea-4f27-896e-a45d97a252ea",
        Date: "2020-06-04T00:00:00.000Z",
        transactionId: "14",
        transactionType: "Payment",
        amount: 1500,
      ,
    ],
  ,
  
    Date: "2020-06",
    data: [
      
        id: "cf44e27f-2111-462d-b3bd-420a193745b8",
        Date: "2020-06-02T00:00:00.000Z",
        transactionId: "5",
        transactionType: "Payment",
        amount: 1000,
      ,
    ],
  ,
]

const res = _.chain(data)
  .groupBy("Date")
  .toPairs()
  .map(([key, value]) => (
    Date: key,
    data: _.flatMap(value, "data"),
  ))
  .value()

console.log(JSON.stringify(res, null, 2))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

【讨论】:

以上是关于摸索数组中的相似记录一个键?的主要内容,如果未能解决你的问题,请参考以下文章

MySQL,根据 JSON 数组中的值选择记录

有没有办法告诉 crossfilter 将数组元素视为单独的记录,而不是将整个数组视为单个键?

具有相似数据的重复记录

DataGridView右键菜单自定义显示及隐藏列

没有重定向的 WebView 返回历史记录

永久存储具有512个浮点数和100万条记录的数组以便快速搜索的最佳方法是什么?