gpt4 book ai didi

Javascript - 对对象进行分组和排序

转载 作者:行者123 更新时间:2023-11-30 09:15:07 26 4
gpt4 key购买 nike

我接到了一个挑战,也做了,但我觉得不是一个好办法。我认为有一种捷径可以做同样的事情。

我寻找 MAP、REDUCE 和 FILTERS,但没有找到好的方法。

目标是:

  1. 查找重复交易。
  2. 相似者分组
  3. 对结果进行排序

说明:有时,当向客户收费时,会创建重复的交易。我们需要找到这些交易,以便处理它们。关于交易的所有内容都应该相同,除了交易 ID 和交易发生的时间,因为最多可能会有一分钟的延迟。

findDuplicateTransactions(交易)

查找sourceAccount、targetAccount、类别、金额相同且每笔连续交易时间差小于1分钟的所有交易。

输入您可以假设所有参数将始终存在且有效。但是,不保证传入的交易以任何特定顺序进行。

交易列表(Transaction[])输出所有重复事务组的列表,按时间升序排列 (Transaction[][]) 这些组应按组中第一个事务的升序排序。例子输入:

[
{
id: 3,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:34:30.000Z'
},
{
id: 1,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:33:00.000Z'
},
{
id: 6,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:05.000Z'
},
{
id: 4,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:36:00.000Z'
},
{
id: 2,
sourceAccount: 'A',
targetAccount: 'B',
amount: 100,
category: 'eating_out',
time: '2018-03-02T10:33:50.000Z'
},
{
id: 5,
sourceAccount: 'A',
targetAccount: 'C',
amount: 250,
category: 'other',
time: '2018-03-02T10:33:00.000Z'
}
];

期望输出:

[
[
{
id: 1,
sourceAccount: "A",
targetAccount: "B",
amount: 100,
category: "eating_out",
time: "2018-03-02T10:33:00.000Z"
},
{
id: 2,
sourceAccount: "A",
targetAccount: "B",
amount: 100,
category: "eating_out",
time: "2018-03-02T10:33:50.000Z"
},
{
id: 3,
sourceAccount: "A",
targetAccount: "B",
amount: 100,
category: "eating_out",
time: "2018-03-02T10:34:30.000Z"
}
],
[
{
id: 5,
sourceAccount: "A",
targetAccount: "C",
amount: 250,
category: "other",
time: "2018-03-02T10:33:00.000Z"
},
{
id: 6,
sourceAccount: "A",
targetAccount: "C",
amount: 250,
category: "other",
time: "2018-03-02T10:33:05.000Z"
}
]
];

这是我的代码,但我不喜欢它。有什么好的办法吗?

function findDuplicateTransactions (transactions = []) {
var result = [];

console.info("total itens :" + transactions.length);

//sort
transactions = transactions.sort((a,b)=> a.time.localeCompare(b.time))

//remove itens not duplicated
result = removeItens(transactions);


//group
result = groupBy(result, function(item){
return [item.sourceAccount, item.targetAccount, item.amount, item.category];
});

console.info(result);

//remove UniqueElements
result = removeUniqueElements(result);



return result;
}

function removeUniqueElements(array){
var filtered = array.filter(function(value, index, arr){
return value.length >= 2;
});

return filtered;
}

function removeItens(array){
var itensToBeRemoved = [];
for (var index = 0; index < array.length; index++) {
const element1 = array[index];
var cont = 0;

console.info("============== looking for: " + element1.id);

for (var index2 = 0; index2 < array.length; index2++) {
const element2 = array[index2];

if(element1.id != element2.id){


var date1 = new Date(element1.time);
var date2 = new Date(element2.time);

var timeDiff = Math.abs(date2.getTime() - date1.getTime());

console.info("comparing :" + element1.id + "<->" + element2.id + " diff: " + timeDiff);


if( timeDiff < 60000) {
//keep it - is similar
console.info("find one duplicated: " + element2.id);
break;
}else{
cont++;
}
}
}

//console.info("cont: " + cont)

if(cont == array.length-1){
//array.splice(index, 1);
console.info("possible duplicated: " + element1.id);
itensToBeRemoved.push(element1.id);
}
}

var filtered = [];

for(var i=0; i<itensToBeRemoved.length; i++){
console.info("remove item: " + itensToBeRemoved[i]);
array = arrayRemove(array, itensToBeRemoved[i]);
}

return array;
}

function arrayRemove(arr, value) {
return arr.filter(function(ele){
console.info("watching: " + ele.id);
console.info("index: " + value);
return ele.id != value;
});
}

function groupBy( array , f ){
var lists = {};
array.forEach( function( o ){
var list = JSON.stringify( f(o) );
lists[list] = lists[list] || [];
lists[list].push( o );
});
return Object.keys(lists).map( function( list ){
return lists[list];
})
}

最佳答案

可以提前对数组进行排序,通过寻找同类的组来缩减数组。

var data = [{ id: 3, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:34:30.000Z' }, { id: 1, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:00.000Z' }, { id: 6, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:05.000Z' }, { id: 4, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:36:00.000Z' }, { id: 2, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:50.000Z' }, { id: 5, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:00.000Z' }],
result = data
.sort(({ time: a }, { time: b }) => a.localeCompare(b))
.reduce((r, o) => {
var temp = r.find(([{ category }]) => category === o.category);
if (!temp) r.push(temp = []);
temp.push(o);
return r;
}, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

用哈希表。

const delta = (t1, t2) => Math.abs(new Date(t1) - new Date(t2));

var data = [{ id: 3, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:34:30.000Z' }, { id: 1, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:00.000Z' }, { id: 6, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:05.000Z' }, { id: 4, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:36:00.000Z' }, { id: 2, sourceAccount: 'A', targetAccount: 'B', amount: 100, category: 'eating_out', time: '2018-03-02T10:33:50.000Z' }, { id: 5, sourceAccount: 'A', targetAccount: 'C', amount: 250, category: 'other', time: '2018-03-02T10:33:00.000Z' }],
keys = ['sourceAccount', 'targetAccount', 'amount', 'category'],
result = Object.values(data
.sort(({ time: a }, { time: b }) => a.localeCompare(b))
.filter((o, i, a) => {
while (a[--i] && delta(o.time, a[i].time) < 60000) {
if (keys.every(k => o[k] === a[i][k])) return;
}
return true;
})
.reduce((r, o) => ((r[o.category] = r[o.category] || []).push(o), r), {})
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于Javascript - 对对象进行分组和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665971/

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