Javascript如何判断数组类型是否为累加类型
How to check if array type is cumulative sum type or not in Javascript
正如我在标题中提到的,我有多个包含一些数据的数组。一些数组具有累积数字,其他数组具有随机数据。我想做的是
逻辑: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)))
}
}
您可以使用 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)
正如我在标题中提到的,我有多个包含一些数据的数组。一些数组具有累积数字,其他数组具有随机数据。我想做的是
逻辑: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)))
}
}
您可以使用 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)