gpt4 book ai didi

javascript - 如何根据对象中的属性合并对象数组中的对象

转载 作者:行者123 更新时间:2023-12-02 23:13:11 26 4
gpt4 key购买 nike

我有一系列事件,例如

this.activities = [
{
id: 54,
event_type: 'CA',
user_id: 56,
user_name: 'Anand',
purpose: disregard,
timestamp: 14356787
},
{
id: 54,
event_type: 'NA',
user_id: 56,
user_name: 'Anand',
purpose: 'privacy',
timestamp: ''
},
{
id: 54,
event_type: 'FA',
user_id: 56,
user_name: 'Anand',
purpose: 'Call',
timestamp: ''
},
{
id: 54,
event_type: 'FA',
user_id: 56,
user_name: 'Anand',
purpose: 'Listen',
timestamp: ''
},
{
id: 54,
event_type: 'CA',
user_id: 56,
user_name: 'Anand',
purpose: 'Not allowed',
timestamp: 14356784
},
{
id: 54,
event_type: 'NA',
user_id: 56,
user_name: 'Anand',
purpose: 'data',
timestamp: 14356786
},
{
id: 54,
event_type: 'CA',
user_id: 56,
user_name: 'Anand',
purpose: 'voicemail',
timestamp: 14356785
},
{
id: 54,
event_type: 'CA',
user_id: 56,
user_name: 'Anand',
purpose: 'phone',
timestamp: 14356775
},
{
id: 54,
event_type: 'CA',
user_id: 56,
user_name: 'Anand',
purpose: 'email',
timestamp: 14356776
},
{
id: 54,
event_type: 'CA',
user_id: 56,
user_name: 'Anand',
purpose: 'letter',
timestamp: 14356777
}
]

我只想根据每个事件的 5ms 时间戳差异,仅按 event_type“CA”对该数组进行分组/过滤。因此,如果 event_type 'CA' 的事件的时间戳彼此相差 5 毫秒以内,则将这些事件组合起来,并形成一个新的事件,除了名为 CA_combined 的组合事件的新 event_type 之外,所有内容都相同。所以这个数组应该像这样

this.activities = [
{
id: 54,
event_type: 'CA_combined',
user_id: 56,
user_name: 'Anand',
purposes: ['disregard', 'Not allowed', 'voicemail'],
timestamp: 14356787
},
{
id: 54,
event_type: 'CA_combined',
user_id: 56,
user_name: 'Anand',
purposes: ['letter','email','phone']
timestamp: 14356777
},
{
id: 54,
event_type: 'NA',
user_id: 56,
user_name: 'Anand',
purpose: 'privacy',
timestamp: ''
},
{
id: 54,
event_type: 'FA',
user_id: 56,
user_name: 'Anand',
purpose: 'Call',
timestamp: ''
},
{
id: 54,
event_type: 'FA',
user_id: 56,
user_name: 'Anand',
purpose: 'Listen',
timestamp: ''
},
{
id: 54,
event_type: 'NA',
user_id: 56,
user_name: 'Anand',
purpose: 'data',
timestamp: 14356786
}
]

我该如何实现这样的目标?我现在的尝试,过滤所有 event_type 为 CA 的事件,并根据时间戳值对数组进行排序

let consentActivities = this.activities.filter(c => {
if (c.event_type === 'CA') {
return true
} else {
return false
}
})
consentActivities.sort((a, b) => {
return b.timestamp - a.timestamp
})

这就是我不知道如何在 5 毫秒内对事件进行分组并完全失去它的地方

 if (consentActivities.length && consentActivities[0].timestamp - consentActivities[consentActivities.length - 1].timestamp <= 5) {
consentActivities[0].purposes = []
consentActivities.forEach((p) => {
consentActivities[0].purposes.push(p.purpose)
})
// consentActivities.splice(1, consentActivities.length)
}

任何意见都将受到高度赞赏

最佳答案

您已经采取了正确的第一步:过滤掉 CA,进行排序,然后您可以通过访问之前的索引轻松进行分组:

  const entries = data
.filter(it => it.event_type === "CA")
.sort((a, b) => a.timestamp - b.timestamp);

const grouped = [];

for(const entry of entries) {
const previous = grouped[grouped.length - 1];
if(previous && Math.abs(entry.timestamp - previous.timestamp) < 5) {
previous.purposes.push(entry.purpose);
} else {
grouped.push({ id: entry.id, /*...*/, purposes: [entry.purpose] });
}
}

关于javascript - 如何根据对象中的属性合并对象数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com