在数组中查找相似的记录是一把钥匙吗



我下面有一个类似的数组

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

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

您可以使用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(。

arr=您的数据集

obj=这将是您的输出

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

首先必须按属性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>

最新更新