在一个数组中查找相似的记录和关键字?

3

我有一个如下的数组

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

请告诉我最优化的做法是什么?

这个回答解决了你的问题吗?按日期对组对象值进行分组 - Yevhen Horbunkov
3个回答

1
你可以使用一个 字典 并轻松检查键是否已经存在,然后追加新数据,否则将日期作为新键添加。
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...
    })
}

我希望这对你有所帮助。使用数组来检查键是否已存在避免重复(默认情况下使用字典/对象)会更加不方便。


请根据我想要的预期输出进行检查。 - TechChain
日期字段是多余的,当然如果你确实需要精确的输出,这并不是一个选择,但它更适合你想要做的事情。而不是一个带有“日期和数据”的对象,你只有一个键来标识他的数据。 - Gray
如果输出无法更改,那么这是一个有效的答案。https://dev59.com/FVkR5IYBdhLWcg3w1gdt#40775082 - Gray

0

首先,您需要按属性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>


0

arr = 你的数据集

obj = 这将是你的输出

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

请根据我想要的预期输出进行检查。 - TechChain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接