所以我擁有的是一組物件
[
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5,
"assign to specialist": 1
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 13,
"duplicate": 5,
"return for changes": 1
},
"changesTotal": 19
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5
},
"changesTotal": 5
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 11,
"return for changes": 1,
"assign to specialist": 1
},
"changesTotal": 13
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 7
},
"changesTotal": 7
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 6
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5,
"assign to specialist": 1
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"assign to specialist": 2,
"edit record": 17
},
"changesTotal": 19
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 2,
"assign to specialist": 2
},
"changesTotal": 4
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 17,
"duplicate": 2
},
"changesTotal": 19
}
]
正如您所看到的,在同一封電子郵件下會有多個條目,這些條目對應于用戶執行某些操作的每個月,因為這些新條目將每個月填充,其中一些大部分來自相同的用戶。我不知道所有的動作,但我可以保證這些動作的拼寫都是一樣的。資料很舊,因此您可以看到有人使用空格作為密鑰,這是另一天的另一項任務。
我想通過同一封電子郵件組合物件,然后將幾個月內的相同操作相加,并添加一個月中存在但另一個月不存在的操作。
所以作為一個例子,[email protected]最終結果看起來像
看起來像
[{
"email": "[email protected]",
"actionsCount": {
"edit record": 16,
"return for changes": 1,
"assign to specialist": 1
}
"changesTotal": 18
}]
因此,因為edit recordis 存在于兩種情況中,actionsCount它們只是簡單地相加,并且return for changes存在assign to specialist于其中一個中而不存在于另一個中
現在我覺得我已經接近我想要實作的目標了?我目前的做法如下
tier2CollectorsActions.reduce((acc, {email, actionsCount}) => {
acc[email] ??= {email: email, actionsCount: []};
if(Array.isArray(actionsCount))
acc[email].actionsCount = _.merge(acc[email].actionsCount, actionsCount)
else
acc[email].actionsCount.push(actionsCount);
return acc;
}, {})
這讓我回來了
{
"email": "[email protected]",
"actionsCount": [
{
"edit record": 5,
"assign to specialist": 1
},
{
"assign to specialist": 2,
"edit record": 17
}
]
}
如您所見,每個actionsCount陣列對應于兩個單獨[email protected]物件的actionsCount資料。我需要的 actionsCount 資料不是作為陣列而是作為物件回傳,我在示例 bc 中將其作為陣列執行,到目前為止,這是我可以從兩者中獲取資料的唯一方法actionCounts。我一直在嘗試使用 Object.value 和 reduce,但我還沒有完全掌握,但我覺得我已經很接近了。
任何方向/提示/建議將不勝感激
uj5u.com熱心網友回復:
你很親密。
- 似乎
actionsCount總是一個物件,而不是一個陣列 - 只需將東西添加到物件
const data = [{
"email": "[email protected]",
"actionsCount": {
"edit record": 5,
"assign to specialist": 1
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 13,
"duplicate": 5,
"return for changes": 1
},
"changesTotal": 19
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5
},
"changesTotal": 5
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 11,
"return for changes": 1,
"assign to specialist": 1
},
"changesTotal": 13
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 7
},
"changesTotal": 7
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 6
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5,
"assign to specialist": 1
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"assign to specialist": 2,
"edit record": 17
},
"changesTotal": 19
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 2,
"assign to specialist": 2
},
"changesTotal": 4
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 17,
"duplicate": 2
},
"changesTotal": 19
}
]
const result = data.reduce((acc, {
email,
actionsCount,
changesTotal
}) => {
acc[email] ??= {
email: email,
actionsCount: {},
changesTotal: 0
};
Object.entries(actionsCount).forEach(([key, value]) => {
acc[email].actionsCount[key] ??= 0
acc[email].actionsCount[key] = value
})
acc[email].changesTotal = changesTotal
return acc;
}, {})
console.log(result)
uj5u.com熱心網友回復:
使用 lodash's_.groupBy()通過電子郵件對專案進行分組,然后映射組并使用_.mergeWith(). 在_.mergeWith()檢查任何一個值是否是數字的定制器功能中(因為其中一個可能是undefined),如果它們是數字,則將它們相加。如果沒有,請_.mergeWith()通過 return 處理它們undefined:
const { flow, groupBy, map, mergeWith, isNumber } = _
const fn = flow(
arr => groupBy(arr, 'email'),
groups => map(groups, items => mergeWith(...items, (o1, o2) =>
isNumber(o1) || isNumber(o2) ? (o1 ?? 0) (o2 ?? 0) : undefined
))
)
const arr = [{"email":"[email protected]","actionsCount":{"edit record":5,"assign to specialist":1},"changesTotal":6},{"email":"[email protected]","actionsCount":{"edit record":13,"duplicate":5,"return for changes":1},"changesTotal":19},{"email":"[email protected]","actionsCount":{"edit record":5},"changesTotal":5},{"email":"[email protected]","actionsCount":{"edit record":11,"return for changes":1,"assign to specialist":1},"changesTotal":13},{"email":"[email protected]","actionsCount":{"edit record":7},"changesTotal":7},{"email":"[email protected]","actionsCount":{"edit record":6},"changesTotal":6},{"email":"[email protected]","actionsCount":{"edit record":5,"assign to specialist":1},"changesTotal":6},{"email":"[email protected]","actionsCount":{"assign to specialist":2,"edit record":17},"changesTotal":19},{"email":"[email protected]","actionsCount":{"edit record":2,"assign to specialist":2},"changesTotal":4},{"email":"[email protected]","actionsCount":{"edit record":17,"duplicate":2},"changesTotal":19}]
const result = fn(arr)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG ljU96qKRCWh quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
uj5u.com熱心網友回復:
您不應該初始化actionsCount為陣列。這導致它將每個actionsCount物件推送到陣列上,而不是組合屬性。
您需要遍歷 中的屬性actionsCount,將它們添加到 中的相應值acc[email]。
const tier2CollectorsActions = [{
"email": "[email protected]",
"actionsCount": {
"edit record": 5,
"assign to specialist": 1
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 13,
"duplicate": 5,
"return for changes": 1
},
"changesTotal": 19
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5
},
"changesTotal": 5
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 11,
"return for changes": 1,
"assign to specialist": 1
},
"changesTotal": 13
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 7
},
"changesTotal": 7
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 6
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 5,
"assign to specialist": 1
},
"changesTotal": 6
},
{
"email": "[email protected]",
"actionsCount": {
"assign to specialist": 2,
"edit record": 17
},
"changesTotal": 19
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 2,
"assign to specialist": 2
},
"changesTotal": 4
},
{
"email": "[email protected]",
"actionsCount": {
"edit record": 17,
"duplicate": 2
},
"changesTotal": 19
}
];
const result = tier2CollectorsActions.reduce((acc, {
email,
actionsCount
}) => {
acc[email] ??= {
email: email,
actionsCount: {}
};
Object.entries(actionsCount).forEach(([key, value]) => {
acc[email].actionsCount[key] = (acc[email].actionsCount[key] || 0) value;
});
return acc;
}, {})
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/513979.html
