【问题标题】:How to check if array type is cumulative sum type or not in Javascript如何在Javascript中检查数组类型是否为累积和类型
【发布时间】:2021-10-13 22:41:16
【问题描述】:

正如我在标题中提到的,我有多个包含一些数据的数组。一些数组具有累积数字,其他数组具有随机数据。我想做的是,
逻辑:if an array is not cumulative, then make it cumulative.

//Randomized arrays are
arrays= [[
  '0.029', '0.029', '0.030', '0.030', '0.031', '0.031',
  '0.032', '0.032', '0.033', '0.034', '0.034', '0.034',
  '0.039', '0.039', '0.001',
  '0.001', '0.002', '0.003', '0.003', '0.004', '0.004',
  '0.005', '0.006', '0.006', '0.007', '0.007', '0.008',
  '0.011', '0.012', '0.012', '0.013', '0.014', '0.014',
  '0.015', '0.015', '0.016', '0.017'
],
[
  '0.002', '0.002', '0.002', '0.002',
  '0.003', '0.003', '0.003', '0.003',
  '0.003', '0.004', '0.004', '0.004',
  '0.000', '0.000', '0.000', '0.001',
  '0.001', '0.001', '0.001', '0.001',
  '0.001', '0.001', '0.001', '0.002',
  '0.002', '0.002', '0.002', '0.002',
  '0.003', '0.003', '0.003'
],
// Cumulative array
[
  '0.002', '0.004', '0.006', '0.008',
  '0.011', '0.014', '0.017', '0.020',
  '0.023', '0.027', '0.031', '0.035',
  '0.035', '0.035', '0.035', '0.036',
  '0.037', '0.038', '0.039', '0.040',
  '0.041', '0.042', '0.043', '0.045',
  '0.047', '0.049', '0.051', '0.053',
  '0.055', '0.057', '0.059', '0.061',
  '0.064', '0.067', '0.070'
]]

我试过这样。

var lastData=[]
for (i in arrays) {
// I thought that if array still includes 0 data inside, than make it cumulative
   if (arrays[i].indexOf("0.000") !== -1) {
       console.log(i, ' is cumulative'
       lastData = arrays[i].map((elem, index) =>
                  arrays[i].slice(0, index + 1)
                           .reduce((a, b) => 
                            (parseFloat(a) + parseFloat(b)).toFixed(3)))
   }
}

【问题讨论】:

    标签: javascript arrays cumulative-sum


    【解决方案1】:

    您可以使用Array.map() 和自定义函数isCumulative 来确定每个数组是否连续增加。

    在本例中,我们将使用Array.every() 来确保每个数字都在增加:

    arrays= [[
      '0.029', '0.029', '0.030', '0.030', '0.031', '0.031',
      '0.032', '0.032', '0.033', '0.034', '0.034', '0.034',
      '0.039', '0.039', '0.001',
      '0.001', '0.002', '0.003', '0.003', '0.004', '0.004',
      '0.005', '0.006', '0.006', '0.007', '0.007', '0.008',
      '0.011', '0.012', '0.012', '0.013', '0.014', '0.014',
      '0.015', '0.015', '0.016', '0.017'
    ],
    [
      '0.002', '0.002', '0.002', '0.002',
      '0.003', '0.003', '0.003', '0.003',
      '0.003', '0.004', '0.004', '0.004',
      '0.000', '0.000', '0.000', '0.001',
      '0.001', '0.001', '0.001', '0.001',
      '0.001', '0.001', '0.001', '0.002',
      '0.002', '0.002', '0.002', '0.002',
      '0.003', '0.003', '0.003'
    ],
    // Cumulative array
    [
      '0.002', '0.004', '0.006', '0.008',
      '0.011', '0.014', '0.017', '0.020',
      '0.023', '0.027', '0.031', '0.035',
      '0.035', '0.035', '0.035', '0.036',
      '0.037', '0.038', '0.039', '0.040',
      '0.041', '0.042', '0.043', '0.045',
      '0.047', '0.049', '0.051', '0.053',
      '0.055', '0.057', '0.059', '0.061',
      '0.064', '0.067', '0.070'
    ],
    [
       '0.000','0.001','0.002','0.003' 
    ],
    [
       '0.000','0.002','0.001','0.003' 
    ]
    ]
    
    // Return true if elements in the array are monotonically increasing _or_ equal to previous element.
    function isCumulative(arr) {
        return arr.every(greaterThanOrEqual);
    }
    
    function greaterThanOrEqual(el, idx, arr) {
        const prevElement = arr[idx-1];
        return !idx || +el >= +prevElement;
    }
    
    const result = arrays.map(isCumulative);
    console.log('Result:', result)

    对于单个数组:

    function greaterThanOrEqual(el, idx, arr) {
        const prevElement = arr[idx-1];
        return !idx || +el >= +prevElement;
    }
    
    const singleArray = ['0.01', '0.02', '0.03'];
    const result = singleArray.every(greaterThanOrEqual);
    console.log('singleArray result:', result)

    【讨论】:

    • 谢谢你的回答,但我们不能改变数据的顺序,排序改变索引,它给出了错误的结果。只是我需要检查is it cumulative?
    • 它在哪里对您的数组进行排序?你在说什么?问题的字面意思是如何check 他们是正确的
    • 嘿@TheBombSquad,我已经更新了答案,希望能澄清一点!
    • @apsisxcoder 如果你想让你的数组累积,如果不是,它会像if(!isCumulative(myArr)){ myArr.sort((a,b)=>a-b) }
    • @TerryLennox,那么我们如何检查这个函数中的单个数组呢?我直接尝试了const result = singleArray.map(greaterThanOrEqual),它给了我[ true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2016-11-24
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多