Javascript 在帐户数组中查找重复项
挑战描述如下:Javascript 在帐户数组中查找重复项,javascript,Javascript,挑战描述如下: [ { 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, catego
[
{
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'
}
];
查找具有相同sourceAccount、targetAccount、类别、金额且每个连续事务之间的时间差小于1分钟的所有事务
输入如下:
[
{
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 = []) {
return transactions.map(t => ({
key: JSON.stringify([t.sourceAccount, t.targetAccount, t.amount, t.category]),
t
})).sort((a,b) =>
a.t.time.localeCompare(b.t.time)).sort((a, b) =>a.key < b.key ? -1 : 1)}
我错过了最后一步,在这一步中,我应该相互比较并形成数组。我猜reduce是正常的,但我不能完全相信它。有人能帮忙吗?考虑到事务是一个对象,它可以被教导说它是否是另一个事务的dup
常量transactionData=[{
id:3,
sourceAccount:'A',
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:34:30.000Z”
},
{
id:1,
sourceAccount:'A',
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:33:00.000Z”
},
{
id:6,
sourceAccount:'A',
targetAccount:'C',
金额:250,
类别:'其他',
时间:“2018-03-02T10:33:05.000Z”
},
{
id:4,
sourceAccount:'A',
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:36:00.000Z”
},
{
id:2,
sourceAccount:'A',
目标帐户:“B”,
金额:100,
类别:“外出就餐”,
时间:“2018-03-02T10:33:50.000Z”
},
{
id:5,
sourceAccount:'A',
targetAccount:'C',
金额:250,
类别:'其他',
时间:“2018-03-02T10:33:00.000Z”
}
]
类事务{
构造器{
Object.assignthis,obj
}
isDupOfxaction{
回来
this.id!==xaction.id&&//事务本身不是重复的
this.sourceAccount==xaction.sourceAccount&&
this.targetAccount==xaction.targetAccount&&
this.category==xaction.category&&
this.amount==xaction.amount&&
这个。大约有一个动作
}
有大约相同的动作时间{
让msDiff=Math.absnew Datethis.time-new Datexaction.time
返回msDiff<60000
}
}
//将数据转换为对象
const transactions=transactionData.mapd=>new Transactiond
//这可能是事务上的静态方法
函数IsDupWithintransactionsAction{
return transactions.somet=>t.isDupOfxaction
}
const dups=transactions.filtert=>isduwhithintransactionst
console.logdups.sorta,b=>a.id-b.id首先使用相同的值映射数组,然后根据时间对其排序,如下所示: var arr=[{ id:3, sourceAccount:'A', 目标帐户:“B”, 金额:100, 类别:“外出就餐”, 时间:“2018-03-02T10:34:30.000Z” }, { id:1, sourceAccount:'A', 目标帐户:“B”, 金额:100, 类别:“外出就餐”, 时间:“2018-03-02T10:33:00.000Z” }, { id:6, sourceAccount:'A', targetAccount:'C', 金额:250, 类别:'其他', 时间:“2018-03-02T10:33:05.000Z” }, { id:4, sourceAccount:'A', 目标帐户:“B”, 金额:100, 类别:“外出就餐”, 时间:“2018-03-02T10:36:00.000Z” }, { id:2, sourceAccount:'A', 目标帐户:“B”, 金额:100, 类别:“外出就餐”, 时间:“2018-03-02T10:33:50.000Z” }, { id:5, sourceAccount:'A', targetAccount:'C', 金额:250, 类别:'其他', 时间:“2018-03-02T10:33:00.000Z” } ]; 函数findDuplicateTransactionstransactions=[]{ return transactions.reduceini,curr,idx=>{ 如果ini[ 当前源帐户+ 当前目标帐户+ 货币金额+ 货币类别]{ ini[当前来源帐户+当前目标帐户+当前金额+当前类别] .pushcurr; }否则{ 伊尼[ 当前源帐户+ 当前目标帐户+ 货币金额+ 货币类别]=[货币]; } 返回ini; }, []; } var结果=Object.valuesfindDuplicateTransactionsarr; var expected=result.mapx=>sortDatex.mapy=>checkTimeDiffy; 函数checkTimeDiffarray{ var returnArr=[]; returnArr.pusharray[0]; 对于变量i=0;i
function findDuplicateTransactions (transactions = []) {
return transactions.map(t => ({
key: JSON.stringify([t.sourceAccount, t.targetAccount, t.amount, t.category]),
dt: new Date(t.time).valueOf(),
t
})).sort((a,b) =>
a.t.time.localeCompare(b.t.time)).sort((a, b) =>a.key < b.key ? -1 : 1)
}
var sortedArray = findDuplicateTransactions(transactionData);
var filteredArray = [];
for (var i=0; i<sortedArray.length; i++) {
if ((i<sortedArray.length-1 && sortedArray[i].key == sortedArray[i+1].key && sortedArray[i+1].dt - sortedArray[i].dt < 60000) ||
(i>0 && sortedArray[i].key == sortedArray[i-1].key && sortedArray[i].dt - sortedArray[i-1].dt < 60000))
filteredArray.push(sortedArray[i]);
}
var masterArray = [];
var subArray = [];
var lastkey = "";
for (var i=0; i<filteredArray.length; i++) {
if (filteredArray[i].key != lastkey) {
if (subArray.length>0) masterArray.push(subArray);
subArray = [];
}
subArray.push(filteredArray[i]);
lastkey = filteredArray[i].key;
}
masterArray.push(subArray);
超过1分钟的事务会发生什么情况?结果中没有包括它们吗?是的,它们被省略了。也许你可以从中获得一些见解?它并不真正起作用,因为它包括id4,与同一类别中的其他值相比,id4超过了一分钟。而且dups数组看起来也不像我的输出。编辑:1忘记匹配数量,2也按id排序,我想你会明白的。它看起来仍然不像预期的输出。输出有两个嵌套数组,这一个只是一个对象数组。谢谢,但这不起作用:它仍然包括id4,与其他数组相比差异大于一分钟。@EdgardoMinotauro,只需添加一个类似上面的时差函数,检查编辑好的功能回答。我决定保释,感觉到我是一个受雇的程序员,在这里,工资很低。