猿问

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

我有一系列的活动,比如


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”的活动的时间戳彼此相差在 5ms 之内,那么将这些活动分组并形成一个新活动,除了称为 CA_combined 的俱乐部活动的新 event_type 之外,所有内容都相同。


慕哥9229398
浏览 220回答 2
2回答

慕尼黑5688855

解决了这个 O(n) 解决方案&nbsp;sortedCAarray.forEach((act, index) => {&nbsp; &nbsp; &nbsp; &nbsp; const currActivityTime = moment(act.timestamp).valueOf()&nbsp; &nbsp; &nbsp; &nbsp; console.log(currActivityTime)&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; First element is already added, avoid duplicates&nbsp; &nbsp; &nbsp; &nbsp; if (index < 1) return&nbsp; &nbsp; &nbsp; &nbsp; // Grab the last key from the timestampMap&nbsp; &nbsp; &nbsp; &nbsp; const timestampKeys = Object.keys(timestampMap)&nbsp; &nbsp; &nbsp; &nbsp; const key = timestampKeys[timestampKeys.length - 1]&nbsp; &nbsp; &nbsp; &nbsp; // Add the activity and purpose to the respective arrays and set for that key if the key is within 5 units of the current activity timestamp&nbsp; &nbsp; &nbsp; &nbsp; if (Math.abs(key - currActivityTime) < 5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let activities = timestampMap[key].activities&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let purposes = timestampMap[key].purposes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activities.push(act)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; purposes.add(act.purpose)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let timestamp = moment(activities[activities.length - 1].timestamp).valueOf()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Edge case to to include all activities in the same bucket if they were within 5 ms of any element within the bucket&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (timestamp > key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperty(timestampMap, timestamp,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.getOwnPropertyDescriptor(timestampMap, key))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete timestampMap[key]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If not just create a new key with the current activity timestamp as the key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timestampMap[currActivityTime] = { activities: [act], purposes: new Set([act.purpose]) }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; })希望对此有任何改进!谢谢
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答