Javascript:比较和重构数组自身的值

l7mqbcuq  于 2022-12-28  发布在  Java
关注(0)|答案(2)|浏览(104)

我有一个带键的数组:timestampprice。时间戳是每小时的,我还使用date-fns库来比较时间戳是否属于同一天-如果是,我将尝试对当天的所有价格点求和。但是,我发现了一些差异:
为清楚起见,阵列分为3天:

{
  "prices": [
    [1671818476949, 16854.460790677775],
    [1671822082820, 16845.20394128685],
    [1671825684227, 16857.05646176291],
    [1671829225308, 16829.569665685623],
    [1671832882885, 16825.560754591264],
    [1671836452315, 16802.19013067553],
    [1671840055897, 16791.45543916491],
    [1671843601067, 16828.5039057286],
    [1671847235412, 16818.886333963925],
    [1671850908752, 16828.80915767001],
    [1671854492281, 16849.1353436805],
    [1671858107673, 16844.63646922823],
    [1671861614876, 16826.94422938057],

    [1671865308427, 16830.32100715682],
    [1671868880378, 16859.602706687205],
    [1671872427084, 16838.84722581856],
    [1671876070308, 16843.698114347266],
    [1671879705628, 16833.179777617614],
    [1671883320221, 16826.127986844378],
    [1671886841429, 16835.60904589224],
    [1671890403652, 16826.52735073518],
    [1671894003774, 16840.053047104313],
    [1671897642888, 16844.75966886341],
    [1671901289954, 16846.46639955969],
    [1671904904119, 16846.743046196585],
    [1671908487819, 16849.228682191264],
    [1671912082408, 16853.84096165539],
    [1671915617341, 16848.57056951711],
    [1671919248222, 16838.55847898353],
    [1671922845055, 16847.802247651052],
    [1671926406952, 16848.649159225337],
    [1671930105411, 16849.932721831166],
    [1671933659969, 16848.512350420737],
    [1671937267311, 16836.58057734161],
    [1671940900097, 16842.511784308153],
    [1671944512405, 16837.250031512558],
    [1671948089796, 16835.01977126436],

    [1671951658960, 16837.930437518276],
    [1671955200553, 16831.66911474702],
    [1671958801135, 16841.3948238686],
    [1671962500002, 16839.084643107144],
    [1671966068189, 16834.494972743123],
    [1671969625433, 16833.460321328374],
    [1671973226717, 16832.79193114006],
    [1671976864726, 16814.494641690155],
    [1671980509077, 16781.822451943077],
    [1671984031113, 16821.199913544602],
    [1671987680236, 16820.32217610258],
    [1671991240419, 16807.414662482362],
    [1671994911250, 16794.108452870256],
    [1671998512051, 16769.85904421313],
    [1672002110939, 16772.940684988003],
    [1672005625248, 16820.196694166687],
    [1672009311744, 16813.158456662797],
    [1672012841914, 16842.72026144195],
    [1672016506486, 16843.359409890574],
    [1672020041689, 16852.772718398166],
    [1672023659844, 16883.023913256362],
    [1672027284022, 16895.732317171107],
    [1672030862669, 16882.371697632705],

    [1672034480606, 16862.501900560783],
    [1672038111850, 16853.824750006406],
    [1672041654416, 16845.9569876033],
    [1672045227499, 16843.843448135598],
    [1672048916306, 16848.912074255015],
    [1672052466904, 16859.749930767142],
    [1672056072318, 16865.679924348886],
    [1672059600720, 16862.520948313442],
    [1672063223636, 16840.16651774838],
    [1672066882254, 16836.402019038687],
    [1672070414455, 16818.749629565693],
    [1672074026355, 16841.011497434578],
    [1672077687367, 16841.16468121544],
    [1672081285285, 16823.19653988069],
    [1672084914724, 16846.539413040075],
    [1672088451149, 16844.521298565145],
    [1672092105680, 16839.48881574047],
    [1672095707553, 16843.692990272582],
    [1672099308191, 16895.753539841488],
    [1672102868807, 16869.94367902591],
    [1672106436652, 16869.681983132617],
    [1672110025423, 16889.298174537],
    [1672113676425, 16869.264589868446],
    [1672117308026, 16890.08282608721],
    [1672120880596, 16875.43904573174],

    [1672124490175, 16876.94990058372],
    [1672128063768, 16869.15484593176],
    [1672131709243, 16861.249964108203],
    [1672135267379, 16886.480841768516],
    [1672138903770, 16865.316599966118],
    [1672142409979, 16835.8325998708],
    [1672146052583, 16808.706816185026],
    [1672149645559, 16825.2039722797],
    [1672153287438, 16777.896913134075],
    [1672156909469, 16795.71982274382],
    [1672160506962, 16778.505439723354]
  ]
}

我尝试了以下方法:

let sumPrice = 0;
let priceArr = [];
for (let i = 0; i < coinHistory.prices?.length; i++) {
  const coin = coinHistory.prices[i];
  const currentDate = coin[0];
  const currentPrice = coin[1];
  if (i === 0) {
    sumPrice = currentPrice;
  }

  if (i >= 1) {
    const prevDate = coinHistory.prices[i - 1][0];
    const prevPrice = coinHistory.prices[i - 1][1];
    const stillSameDay = isSameDay(currentDate,prevDate);

      if (stillSameDay) {
         sumPrice += currentPrice;
      } else{
        priceArr.push({timeStampDate: prevDate 
        ,totalDailySum: sumPrice});
        sumPrice = currentPrice;
      }
  }
}

我得到的结果是:

[
{timeStampDate: 1671861614876, totalDailySum: 218802.4126234967},
{timeStampDate: 1671948089796, totalDailySum: 404208.3927127256}, 
{timeStampDate: 1672034480606, totalDailySum: 403928.82564146793}, 
{timeStampDate: 1672120880596, totalDailySum: 404514.88530415593}
]

如您所见,结果不包括从1672124490175开始的时间戳,1672124490175是该月的第27天,与1672120880596是该月的第26天不同。
预期输入:

[
{timeStampDate: 1671861614876, totalDailySum: TOTAL_PRICES_FOR_ALL_MATHING_TIMESTAMPS},
{timeStampDate: 1671948089796, totalDailySum: TOTAL_PRICES_FOR_ALL_MATHING_TIMESTAMPS}, 
{timeStampDate: 1672034480606, totalDailySum: TOTAL_PRICES_FOR_ALL_MATHING_TIMESTAMPS}, 
{timeStampDate: 1672120880596, totalDailySum: TOTAL_PRICES_FOR_ALL_MATHING_TIMESTAMPS},
{timeStampDate: 1672160506962, totalDailySum: TOTAL_PRICES_FOR_ALL_MATHING_TIMESTAMPS}
]
7fhtutme

7fhtutme1#

你的代码很好。只是忘了一个小细节:如果你遇到一个新的日期,你只在结果数组中存储一个值。2但是在最后一天之后,就不会遇到新的日期,最后一天的数据也不会存储在数组中。
要解决这个问题,您可以添加一个特例,将最后一天的数据推送到数组中:

if (i + 1 === coinHistory.prices.length) {
  priceArr.push({
    timeStampDate: currentDate,
    totalDailySum: sumPrice
  });
}

下面是我用来测试它的完整代码:
x一个一个一个一个x一个一个二个x
https://jsfiddle.net/timlg07/0Ldnt6ko/

iszxjhcz

iszxjhcz2#

这是一个多步骤问题。
1.按日期对元组值数组进行分组(请参见Array.prototype.group() - JavaScript | MDN
这实际上比看起来要复杂得多。日历“日期”是午夜(包括午夜)和午夜(不包括午夜)之间的一段时间**,根据给定的时区**。您没有指定是否要使用通用协调时间或某个任意时区(例如本地设备的时区),但该决定可能会在很大程度上改变结果。
1.按日期对元组进行分组后,必须迭代每个日期的数组以计算总和,然后将结果推入一个新数组,就像您为所需输出描述的那样
下面的代码片段演示了如何使用一些函数模式执行上述步骤。与日期相关的函数接受一个布尔参数,该参数允许您使用本地时区。

function group (array, selector) {
  const result = {};
  for (const element of array) {
    const key = selector(element);
    (result[key] ??= []).push(element);
  }
  return result;
}

function pad (num) {
  return String(num).padStart(2, '0');
}

function getDateKey (timestamp, useLocalTimeZone) {
  const date = new Date(timestamp);
  const year = useLocalTimeZone ? date.getFullYear() : date.getUTCFullYear();
  const month = (useLocalTimeZone ? date.getMonth() : date.getUTCMonth()) + 1;
  const dayOfMonth = useLocalTimeZone ? date.getDate() : date.getUTCDate();
  return `${year}-${pad(month)}-${pad(dayOfMonth)}`;
}

function groupAndSum (coinHistory, useLocalTimeZone) {
  const groupedByDate = group(
    coinHistory.prices,
    ([timestamp]) => getDateKey(timestamp, useLocalTimeZone),
  );

  const result = [];

  for (const [calendarDate, tuples] of Object.entries(groupedByDate)) {
    let totalDailySum = 0;
    for (const [,price] of tuples) totalDailySum += price;
    result.push({calendarDate, totalDailySum});
  }

  return result;
}

const coinHistory = {"prices":[[1671818476949,16854.460790677775],[1671822082820,16845.20394128685],[1671825684227,16857.05646176291],[1671829225308,16829.569665685623],[1671832882885,16825.560754591264],[1671836452315,16802.19013067553],[1671840055897,16791.45543916491],[1671843601067,16828.5039057286],[1671847235412,16818.886333963925],[1671850908752,16828.80915767001],[1671854492281,16849.1353436805],[1671858107673,16844.63646922823],[1671861614876,16826.94422938057],[1671865308427,16830.32100715682],[1671868880378,16859.602706687205],[1671872427084,16838.84722581856],[1671876070308,16843.698114347266],[1671879705628,16833.179777617614],[1671883320221,16826.127986844378],[1671886841429,16835.60904589224],[1671890403652,16826.52735073518],[1671894003774,16840.053047104313],[1671897642888,16844.75966886341],[1671901289954,16846.46639955969],[1671904904119,16846.743046196585],[1671908487819,16849.228682191264],[1671912082408,16853.84096165539],[1671915617341,16848.57056951711],[1671919248222,16838.55847898353],[1671922845055,16847.802247651052],[1671926406952,16848.649159225337],[1671930105411,16849.932721831166],[1671933659969,16848.512350420737],[1671937267311,16836.58057734161],[1671940900097,16842.511784308153],[1671944512405,16837.250031512558],[1671948089796,16835.01977126436],[1671951658960,16837.930437518276],[1671955200553,16831.66911474702],[1671958801135,16841.3948238686],[1671962500002,16839.084643107144],[1671966068189,16834.494972743123],[1671969625433,16833.460321328374],[1671973226717,16832.79193114006],[1671976864726,16814.494641690155],[1671980509077,16781.822451943077],[1671984031113,16821.199913544602],[1671987680236,16820.32217610258],[1671991240419,16807.414662482362],[1671994911250,16794.108452870256],[1671998512051,16769.85904421313],[1672002110939,16772.940684988003],[1672005625248,16820.196694166687],[1672009311744,16813.158456662797],[1672012841914,16842.72026144195],[1672016506486,16843.359409890574],[1672020041689,16852.772718398166],[1672023659844,16883.023913256362],[1672027284022,16895.732317171107],[1672030862669,16882.371697632705],[1672034480606,16862.501900560783],[1672038111850,16853.824750006406],[1672041654416,16845.9569876033],[1672045227499,16843.843448135598],[1672048916306,16848.912074255015],[1672052466904,16859.749930767142],[1672056072318,16865.679924348886],[1672059600720,16862.520948313442],[1672063223636,16840.16651774838],[1672066882254,16836.402019038687],[1672070414455,16818.749629565693],[1672074026355,16841.011497434578],[1672077687367,16841.16468121544],[1672081285285,16823.19653988069],[1672084914724,16846.539413040075],[1672088451149,16844.521298565145],[1672092105680,16839.48881574047],[1672095707553,16843.692990272582],[1672099308191,16895.753539841488],[1672102868807,16869.94367902591],[1672106436652,16869.681983132617],[1672110025423,16889.298174537],[1672113676425,16869.264589868446],[1672117308026,16890.08282608721],[1672120880596,16875.43904573174],[1672124490175,16876.94990058372],[1672128063768,16869.15484593176],[1672131709243,16861.249964108203],[1672135267379,16886.480841768516],[1672138903770,16865.316599966118],[1672142409979,16835.8325998708],[1672146052583,16808.706816185026],[1672149645559,16825.2039722797],[1672153287438,16777.896913134075],[1672156909469,16795.71982274382],[1672160506962,16778.505439723354]]};

const outputUtc = groupAndSum(coinHistory);
console.log('UTC:', outputUtc);

const outputLocal = groupAndSum(coinHistory, true);
console.log('Local:', outputLocal);

相关问题