如何获取数组对象nodejs中最后一次的值

how to get last time of values in array object nodejs

我有一个具有以下值的市场报价数组对象:

candles['AUDIOUSDT'] = [
    {
        t: 1649936820000,
        o: 41044.99,
        c: 41052.21,
        h: 41063.84,
        l: 41044.99,
        v: 1.2067
    },
    {
        t: 1649936820000,
        o: 41044.99,
        c: 41045,
        h: 41063.84,
        l: 41044.99,
        v: 1.3728
    },
    {
        t: 1649936880000,
        o: 41044.99,
        c: 41045,
        h: 41063.84,
        l: 41044.99,
        v: 0.1
    },
    {
        t: 1649936880000,
        o: 41044,
        c: 41049,
        h: 41049,
        l: 41011,
        v: 1
    }
]

我想在数组对象中包含每次的最后时间:

candles['AUDIOUSDT'] = [
{
    t: 1649936820000,
    o: 41060.01,
    c: 41045,
    h: 41063.84,
    l: 41044.99,
    v: 1.3728
},
{
    t: 1649936880000,
    o: 41044,
    c: 41049,
    h: 41049,
    l: 41011,
    v: 1
}

基本上,如果 t、o、c、h、l、v 是同一时间,我想合并值,关于如何优雅地执行此操作有什么想法吗?

提前致谢

所以,它只需要你做一个向后循环并检查“t”值是否已经在一个唯一的时间列表中。如果是,它将跳过它,否则它将把整个对象添加到一个新的 uniques 数组中来保存结果。

// where the unique times will be held
let unique_times = [];
// where the unique results will be held
let uniques = [];
// loop through the array backwards
for (var i = candles['AUDIOUSDT'].length; i--;) {
  // if current time in object is already in the unique_times array, skip over it
  if (unique_times.indexOf(candles['AUDIOUSDT'][i].t) > -1) {continue;}
  // adds the current time to this array, so the loop knows it's been added and doesn't count it again
  unique_times.push(candles['AUDIOUSDT'][i].t);
  // adds the current object to the new results
  uniques.push(candles['AUDIOUSDT'][i]);
}
// 'uniques' holds the filtered array of objects
console.log(uniques);