我正在尝试从2d javascript数组计算加权平均值。 这是数组:
var timestamp = [
[1524751200, 6000],
[1556287200, 6000],
[1587909600, 6000],
[1619445600, 6000],
[1650981600, 3000],
[1682517600, 3000],
[1714140000, 3000],
[1745676000, 3000],
[1777212000, 1500],
[1808748000, 1500],
[1840370400, 1500],
[1871906400, 1500],
[1903442400, 750],
[1934978400, 750],
[1966600800, 750],
[1998136800, 750],
[2029672800, 375],
[2061208800, 375],
[2092831200, 375],
[2124367200, 375],
[2155903200, 187.5],
[2187439200, 187.5],
[2219061600, 187.5],
[2250597600, 187.5],
[2282133600, 93.75],
[2313669600, 93.75],
[2345292000, 93.75],
[2376828000, 93.75],
[2408364000, 46.875],
[2439900000, 46.875],
[2471522400, 46.875],
[2503058400, 46.875],
[2534594400, 23.4375],
[2566130400, 23.4375],
[2597752800, 23.4375],
[2629288800, 23.4375],
[2660824800, 11.71875],
[2692360800, 11.71875],
[2723983200, 11.71875],
[2755519200, 11.71875],
[2787055200, 5.859375],
[2818591200, 5.859375],
[2850213600, 5.859375],
[2881749600, 5.859375],
[2913285600, 2.9296875],
[2944821600, 2.9296875],
[2976444000, 2.9296875],
[3007980000, 2.9296875],
[3039516000, 1.46484375],
[3071052000, 1.46484375],
[3102674400, 1.46484375],
[3134210400, 1.46484375],
[3165746400, 0.732421875],
[3197282400, 0.732421875],
[3228904800, 0.732421875],
[3260440800, 0.732421875],
[3291976800, 0.3662109375],
[3323512800, 0.3662109375],
[3355135200, 0.3662109375],
[3386671200, 0.3662109375],
[3418207200, 0.18310546875],
[3449743200, 0.18310546875],
[3481365600, 0.18310546875],
[3512901600, 0.18310546875],
[3544437600, 0.091552734375],
[3575973600, 0.091552734375],
[3607596000, 0.091552734375],
[3639132000, 0.091552734375],
[3670668000, 0.0457763671875],
[3702204000, 0.0457763671875],
[3733826400, 0.0457763671875],
[3765362400, 0.0457763671875],
[3796898400, 0.02288818359375],
[3828434400, 0.02288818359375],
[3860056800, 0.02288818359375],
[3891592800, 0.02288818359375],
[3923128800, 0.011444091796875],
[3954664800, 0.011444091796875],
[3986287200, 0.011444091796875],
[4017823200, 0.011444091796875],
[4049359200, 0.0057220458984375],
[4080895200, 0.0057220458984375],
[4112431200, 0.0057220458984375],
[4143967200, 0.0057220458984375],
[4175503200, 0.0028610229492188],
[4207039200, 0.0028610229492188],
[4238661600, 0.0028610229492188],
[4270197600, 0.0028610229492188],
[4301733600, 0.0014305114746094],
[4333269600, 0.0014305114746094],
[4364892000, 0.0014305114746094],
[4396428000, 0.0014305114746094],
[4427964000, 0.0007152557373047],
[4459500000, 0.0007152557373047],
[4491122400, 0.0007152557373047],
[4522658400, 0.0007152557373047],
[4554194400, 0.00035762786865235],
[4585730400, 0.00035762786865235],
[4617352800, 0.00035762786865235],
[4648888800, 0.00035762786865235]
];
第1列代表时间戳,每个代表未来100年每年的日期26.04,第2列基本上是每4年减半的值。 必须在第2列的所有值之间计算加权平均值,并且将每个值计入计算的次数就是该值的权重。我必须应用的条件是我从用户输入中获取的日期,并且我只需要计算与所有小于用户输入所给日期的时间戳相对应的值。 我不知道从哪里开始。
答案 0 :(得分:1)
index = 1
处的平均值。
var timestamp=[[1524751200,6000],[1556287200,6000],[1587909600,6000],[1619445600,6000],[1650981600,3000],[1682517600,3000],[1714140000,3000],[1745676000,3000],[1777212000,1500],[1808748000,1500],[1840370400,1500],[1871906400,1500],[1903442400,750],[1934978400,750],[1966600800,750],[1998136800,750],[2029672800,375],[2061208800,375],[2092831200,375],[2124367200,375],[2155903200,187.5],[2187439200,187.5],[2219061600,187.5],[2250597600,187.5],[2282133600,93.75],[2313669600,93.75],[2345292000,93.75],[2376828000,93.75],[2408364000,46.875],[2439900000,46.875],[2471522400,46.875],[2503058400,46.875],[2534594400,23.4375],[2566130400,23.4375],[2597752800,23.4375],[2629288800,23.4375],[2660824800,11.71875],[2692360800,11.71875],[2723983200,11.71875],[2755519200,11.71875],[2787055200,5.859375],[2818591200,5.859375],[2850213600,5.859375],[2881749600,5.859375],[2913285600,2.9296875],[2944821600,2.9296875],[2976444000,2.9296875],[3007980000,2.9296875],[3039516000,1.46484375],[3071052000,1.46484375],[3102674400,1.46484375],[3134210400,1.46484375],[3165746400,0.732421875],[3197282400,0.732421875],[3228904800,0.732421875],[3260440800,0.732421875],[3291976800,0.3662109375],[3323512800,0.3662109375],[3355135200,0.3662109375],[3386671200,0.3662109375],[3418207200,0.18310546875],[3449743200,0.18310546875],[3481365600,0.18310546875],[3512901600,0.18310546875],[3544437600,0.091552734375],[3575973600,0.091552734375],[3607596000,0.091552734375],[3639132000,0.091552734375],[3670668000,0.0457763671875],[3702204000,0.0457763671875],[3733826400,0.0457763671875],[3765362400,0.0457763671875],[3796898400,0.02288818359375],[3828434400,0.02288818359375],[3860056800,0.02288818359375],[3891592800,0.02288818359375],[3923128800,0.011444091796875],[3954664800,0.011444091796875],[3986287200,0.011444091796875],[4017823200,0.011444091796875],[4049359200,0.0057220458984375],[4080895200,0.0057220458984375],[4112431200,0.0057220458984375],[4143967200,0.0057220458984375],[4175503200,0.0028610229492188],[4207039200,0.0028610229492188],[4238661600,0.0028610229492188],[4270197600,0.0028610229492188],[4301733600,0.0014305114746094],[4333269600,0.0014305114746094],[4364892000,0.0014305114746094],[4396428000,0.0014305114746094],[4427964000,0.0007152557373047],[4459500000,0.0007152557373047],[4491122400,0.0007152557373047],[4522658400,0.0007152557373047],[4554194400,0.00035762786865235],[4585730400,0.00035762786865235],[4617352800,0.00035762786865235],[4648888800,0.00035762786865235]];
const calculateAverage = (date) => {
const filter = timestamp.filter(a => a[0] * 1000 < new Date(date));
const average = filter.reduce((r,a) => r + a[1], 0) / filter.length;
return average;
}
console.log(calculateAverage("10/10/2023"))
在1000
中,filter
有一个乘法,因为您提到了这些是将来的日期。因此,我假设第一列以秒而不是毫秒为单位