gpt4 book ai didi

javascript - 如何比较对象数组中每个对象组的时间属性

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

我确实有一个对象数组,每组对象都需要根据它们的时间属性进行过滤,并且在输出中它应该只返回时差小于 1 分钟的对象,并删除大于 1 分钟的对象时差(格式相同且分组)。

更新:每次连续交易之间的时间差应小于 1 分钟。

var grouped = [
[
{"id":29,"sourceAccount":"my_account","targetAccount":"cinema","amount":-580,"category":"other","time":"2018-05-05T20:01:18.000Z"},
{"id":18,"sourceAccount":"my_account","targetAccount":"cinema","amount":-580,"category":"other","time":"2018-04-05T20:01:18.000Z"}
],
[
{"id":38,"sourceAccount":"my_account","targetAccount":"restaurant","amount":-970,"category":"eating_out","time":"2018-05-17T19:52:46.000Z"},
{"id":22,"sourceAccount":"my_account","targetAccount":"restaurant","amount":-970,"category":"eating_out","time":"2018-04-17T19:52:46.000Z"}
],
[
{"id":31,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:55:10.000Z"},
{"id":30,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:54:21.000Z"},
{"id":33,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:57:05.000Z"},
{"id":19,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-04-07T09:54:21.000Z"},
{"id":32,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:56:09.000Z"},
{"id":35,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:58:06.000Z"}
],
[
{"id":14,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:24:40.000Z"},
{"id":15,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:25:10.000Z"},
{"id":13,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:24:00.000Z"},
{"id":9,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-03-04T07:14:20.000Z"},
{"id":2,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-03-01T12:34:00.000Z"},
{"id":5,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-03-02T09:25:20.000Z"}
],
[
{"id":39,"sourceAccount":"my_account","targetAccount":"fitness_club","amount":-610,"category":"other","time":"2018-05-22T11:54:10.000Z"},
{"id":24,"sourceAccount":"my_account","targetAccount":"fitness_club","amount":-610,"category":"other","time":"2018-04-22T11:54:10.000Z"}
],
[
{"id":41,"sourceAccount":"my_account","targetAccount":"cinema","amount":-450,"category":"other","time":"2018-05-23T19:13:10.000Z"},
{"id":26,"sourceAccount":"my_account","targetAccount":"cinema","amount":-450,"category":"other","time":"2018-04-23T19:13:10.000Z"}
],
[
{"id":1,"sourceAccount":"company_x","targetAccount":"my_account","amount":10000,"category":"salary","time":"2018-02-25T08:00:00.000Z"},
{"id":27,"sourceAccount":"company_x","targetAccount":"my_account","amount":10000,"category":"salary","time":"2018-04-25T08:00:00.000Z"},
{"id":16,"sourceAccount":"company_x","targetAccount":"my_account","amount":10000,"category":"salary","time":"2018-03-25T08:10:00.000Z"}
],
[
{"id":20,"sourceAccount":"my_account","targetAccount":"internet_shop","amount":-1650,"category":"other","time":"2018-04-08T21:36:41.000Z"},
{"id":36,"sourceAccount":"my_account","targetAccount":"internet_shop","amount":-1650,"category":"other","time":"2018-05-08T21:36:41.000Z"}
],
[
{"id":39,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-70,"category":"eating_out","time":"2018-05-15T09:12:20.000Z"},
{"id":23,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-70,"category":"eating_out","time":"2018-04-15T09:12:20.000Z"}
],
[
{"id":40,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-850,"category":"groceries","time":"2018-05-20T18:51:31.000Z"},
{"id":25,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-850,"category":"groceries","time":"2018-04-20T18:51:31.000Z"}
],
[
{"id":17,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1870,"category":"groceries","time":"2018-04-05T10:24:30.000Z"},
{"id":28,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1870,"category":"groceries","time":"2018-05-05T10:24:30.000Z"}
],
[
{"id":21,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1690,"category":"groceries","time":"2018-04-10T18:14:10.000Z"},
{"id":37,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1690,"category":"groceries","time":"2018-05-10T18:14:10.000Z"}
]
]

这是我试过的,但是这个仍然有超过1分钟的那些项目(甚至其中一些有超过1个月的时差!)

var result = grouped.map((tr, i) => {
return tr.filter((t, j) => {
if (grouped[i][j - 1]) {
var d1 = Date.parse(t.time);
var d2 = Date.parse(grouped[i][j - 1].time);
return (d1 - d2) <= 60000;
}
return true;
});
});

就在我运行上面的代码之前,我像这样按时间排序:

  grouped.sort(function (a, b) {
return b.time > a.time
});

更新:

基本上,给定对象组的预期输出应该是这样的:

var expectedOutput = [
[
{"id":31,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:55:10.000Z"},
{"id":30,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:54:21.000Z"},
{"id":32,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:56:09.000Z"}
],
[
{"id":14,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:24:40.000Z"},
{"id":15,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:25:10.000Z"},
{"id":13,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:24:00.000Z"}
]
]

真的不知道我哪里做错了,谁能帮我解决这个问题?谢谢

最佳答案

对于给定的数据结构,需要两次方法 reduce一个列表。首先是将帐户项目列表分组的外部列表结构。这些列表中的每一个首先应该按其帐户项目的时间戳(时间属性)排序。第二个减少步骤通过比较当前项目与其上一个和下一个兄弟之间的时间偏移来找到项目的有效范围,以防它们可用。只会收集(严格)有效范围内的项目。

var grouped = [[
{"id":29,"sourceAccount":"my_account","targetAccount":"cinema","amount":-580,"category":"other","time":"2018-05-05T20:01:18.000Z"},
{"id":18,"sourceAccount":"my_account","targetAccount":"cinema","amount":-580,"category":"other","time":"2018-04-05T20:01:18.000Z"}
], [
{"id":38,"sourceAccount":"my_account","targetAccount":"restaurant","amount":-970,"category":"eating_out","time":"2018-05-17T19:52:46.000Z"},
{"id":22,"sourceAccount":"my_account","targetAccount":"restaurant","amount":-970,"category":"eating_out","time":"2018-04-17T19:52:46.000Z"}
], [
{"id":31,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:55:10.000Z"},
{"id":30,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:54:21.000Z"},
{"id":33,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:57:05.000Z"},
{"id":19,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-04-07T09:54:21.000Z"},
{"id":32,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:56:09.000Z"},
{"id":35,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-90,"category":"eating_out","time":"2018-05-07T09:58:06.000Z"}
], [
{"id":14,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:24:40.000Z"},
{"id":15,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:25:10.000Z"},
{"id":13,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-04-01T10:24:00.000Z"},
{"id":9,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-03-04T07:14:20.000Z"},
{"id":2,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-03-01T12:34:00.000Z"},
{"id":5,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-50,"category":"eating_out","time":"2018-03-02T09:25:20.000Z"}
], [
{"id":39,"sourceAccount":"my_account","targetAccount":"fitness_club","amount":-610,"category":"other","time":"2018-05-22T11:54:10.000Z"},
{"id":24,"sourceAccount":"my_account","targetAccount":"fitness_club","amount":-610,"category":"other","time":"2018-04-22T11:54:10.000Z"}
], [
{"id":41,"sourceAccount":"my_account","targetAccount":"cinema","amount":-450,"category":"other","time":"2018-05-23T19:13:10.000Z"},
{"id":26,"sourceAccount":"my_account","targetAccount":"cinema","amount":-450,"category":"other","time":"2018-04-23T19:13:10.000Z"}
], [
{"id":1,"sourceAccount":"company_x","targetAccount":"my_account","amount":10000,"category":"salary","time":"2018-02-25T08:00:00.000Z"},
{"id":27,"sourceAccount":"company_x","targetAccount":"my_account","amount":10000,"category":"salary","time":"2018-04-25T08:00:00.000Z"},
{"id":16,"sourceAccount":"company_x","targetAccount":"my_account","amount":10000,"category":"salary","time":"2018-03-25T08:10:00.000Z"}
], [
{"id":20,"sourceAccount":"my_account","targetAccount":"internet_shop","amount":-1650,"category":"other","time":"2018-04-08T21:36:41.000Z"},
{"id":36,"sourceAccount":"my_account","targetAccount":"internet_shop","amount":-1650,"category":"other","time":"2018-05-08T21:36:41.000Z"}
], [
{"id":39,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-70,"category":"eating_out","time":"2018-05-15T09:12:20.000Z"},
{"id":23,"sourceAccount":"my_account","targetAccount":"coffee_shop","amount":-70,"category":"eating_out","time":"2018-04-15T09:12:20.000Z"}
], [
{"id":40,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-850,"category":"groceries","time":"2018-05-20T18:51:31.000Z"},
{"id":25,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-850,"category":"groceries","time":"2018-04-20T18:51:31.000Z"}
], [
{"id":17,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1870,"category":"groceries","time":"2018-04-05T10:24:30.000Z"},
{"id":28,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1870,"category":"groceries","time":"2018-05-05T10:24:30.000Z"}
], [
{"id":21,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1690,"category":"groceries","time":"2018-04-10T18:14:10.000Z"},
{"id":37,"sourceAccount":"my_account","targetAccount":"supermarket","amount":-1690,"category":"groceries","time":"2018-05-10T18:14:10.000Z"}
]];

function orderByTimeProperty(a, b) {
return ((a.time < b.time && -1) || (a.time > b.time && 1) || 0);
}

function collectListItemsWithLessThanOneMinuteDelta(collector, item, idx, list) {
function isValidTimeDelta(a, b) {
return (Math.abs((new Date(b)).getTime() - (new Date(a)).getTime()) < timeDelta);
}
var
timeDelta = (1000 * 60),
prevItem = list[idx - 1],
nextItem = list[idx + 1];
if (
(prevItem && nextItem && isValidTimeDelta(prevItem.time, item.time) && isValidTimeDelta(item.time, nextItem.time))
|| (nextItem && isValidTimeDelta(item.time, nextItem.time))
|| (prevItem && isValidTimeDelta(prevItem.time, item.time))
) {
collector.push(item);
}
return collector;
}

function collectGroupedListItemsWithLessThanOneMinuteDelta(collector, list) {
list = list.sort(orderByTimeProperty).reduce(collectListItemsWithLessThanOneMinuteDelta, []);

if (list.length >= 1) {
collector.push(list);
}
return collector;
}

var list = grouped.reduce(collectGroupedListItemsWithLessThanOneMinuteDelta, []);

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

关于javascript - 如何比较对象数组中每个对象组的时间属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51474068/

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