Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何比较数组中的连续日期/时间项并基于特定时间进行筛选_Javascript_Arrays_Node.js - Fatal编程技术网

Javascript 如何比较数组中的连续日期/时间项并基于特定时间进行筛选

Javascript 如何比较数组中的连续日期/时间项并基于特定时间进行筛选,javascript,arrays,node.js,Javascript,Arrays,Node.js,我有以下对象数组: var transactions = [ [ {"id":1,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:00.000Z"}, {"id":2,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03

我有以下对象数组:

var transactions = [
[
    {"id":1,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:00.000Z"},
    {"id":2,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:50.000Z"},
    {"id":3,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:34:30.000Z"},
    {"id":4,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:36:00.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"}
]
]
我需要连续比较每个对象的
time
属性,并且只保留每个连续事务之间的时差小于1分钟的属性

数组格式应该保持不变,这是我尝试过的,但运气不好,不起作用。有什么问题吗

 var newArray = transactions.map(g => g.reduce((r, o, i, a) => {
        if (!i || new Date(o.time).getTime() - new Date(a[i - 1].time).getTime() >= 60000) {
            r.push([o]);
        } else {
            r[r.length - 1].push(o);
        }
        return r;
    }, []));
预期输出如下所示:

var output = [
[
    {"id":1,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:00.000Z"},
    {"id":2,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:50.000Z"},
    {"id":3,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","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"}
]
]
您可以
Array#映射源数组
,并且在每次迭代中,
Array#通过比较当前元素的时间和上一个元素的时间来过滤所需元素

var交易=[{“id”:1,“sourceAccount”:“A”,“targetAccount”:“B”,“amount”:100,“category”:“food”,“time”:“2018-03-02T10:33:00.000Z”},{“id”:2,“sourceAccount”:“A”,“targetAccount”:“B”,“amount”:100,“category”:“food”,“time”:“2018-03-02T10:33:50.000Z”},{“id”:3,“sourceAccount”:“A”,“targetAccount”:“B”,“amount”:100,“categegory”:“food”,“time”:“food”,“time”:“2018-03-02T10:34:30.000Z”},{“id”:4,“源帐户”:“A”,“目标帐户”:“B”,“金额”:100,“类别”:“食品”,“时间”:“2018-03-02T10:36:00.000Z”},{“id”:5,“源帐户”:“A”,“目标帐户”:“C”,“金额”:250,“类别”:“其他”,“时间”:“2018-03-02T10:33:00.000Z”},{“id”:6,“源帐户”:“A”,“目标帐户”:“C”,“金额”:250,“类别”:“其他”,“时间”:2018-03-02T10:33:05.000Z“}];
var结果=事务。映射((tr,i)=>{
返回tr.filter((t,j)=>{
if(交易[i][j-1]){
var d1=新日期(t.时间);
var d2=新日期(交易[i][j-1]。时间);
return(d1.getTime()-d2.getTime())您可以
Array#映射
您的源数组,并且在每次迭代中,
Array#通过比较当前元素的时间和上一个元素的时间来过滤
所需的元素

var交易=[{“id”:1,“sourceAccount”:“A”、“targetAccount”:“B”、“amount”:100,“category”:“food”、“time”:“2018-03-02T10:33:00.000Z”},{“id”:2,“sourceAccount”:“A”、“targetAccount”:“B”、“amount”:100,“category”:“food”、“time”:“2018-03-02T10:33:50.000Z”},“id”:3,“sourceAccount”:“A”、“targetAccount”:“B”、“amount”:100,“categegory”:“food”、“time”:“food”、“time”:”2018-03-02T10:34:30.000Z“},{“id”:4,“源帐户”:“A”,“目标帐户”:“B”,“金额”:100,“类别”:“食品”,“时间”:“2018-03-02T10:36:00.000Z”),{“id”:5,“源帐户”:“A”,“目标帐户”:“C”,“金额”:250,“类别”:“其他”,“时间”:“2018-03-02T10:33:00.000Z”},{“id”:6,“源帐户”:“A”,“目标帐户”:“C”,“金额”:250,“类别”:“其他”,“时间”:2018-03-02T10:33:05.000Z“}];
var结果=事务。映射((tr,i)=>{
返回tr.filter((t,j)=>{
if(交易[i][j-1]){
var d1=新日期(t.时间);
var d2=新日期(交易[i][j-1]。时间);

return(d1.getTime()-d2.getTime())请详细说明您试图实现的目标或预期输出遵循的规则。请详细说明您试图实现的目标或预期输出遵循的规则。