使用 JavaScript 在每一列的数组中查找连续重复的数字
Finding continuous repetition of numbers in an array in each column using JavaScript
我有来自 8 个站点的实时温度日志用于科学研究。
每当所有8个站点都获取到数据时,将其推送到一个8列的数组中。
例如以下 table
Time1 22 26 18 12 20 15 .. for 8 stations
Time2 20 26 15 13 20 18...
Time3 19 28 17 15 20 16 ...
时间 1 是最近的数据标签...时间 2 是前一个,依此类推。
此处 26 在第 2 站重复 2 次
20 is repeated 3 times in station #5
所以预期的输出类似于上面的2句话。
我不是一个普通的(学过的).js 程序员,但可以在系统上调整主例程 运行 中的代码。
有没有办法用信号表示站点的温度重复以及重复多少次?
谢谢。
这是一个很好的算法,当然它可以更简单,但我是这样做的。
创建一个包含时间和温度的二维数组。父数组将为 times
,子数组将为 temperatures
每个 station
。每个子数组有 8 个条目。该子数组中的每个位置都表示 station
.
首先,您必须转换数据并为每个 station
.
创建 temperatures
组
[
[22, 20, 19],
[26, 26, 28],
[18, 15, 17],
...
]
遍历新集合并计算每个站点数组中 temperatures
的重复项数。将每个站阵列放在一个对象中,您可以在其中计数。
[
{
"22": 1,
"20": 1,
"19": 1,
},
{
"26": 2,
"28": 1,
},
{
"18": 1,
"15": 1,
"17": 1,
},
...
]
现在过滤掉任何低于 1
的站点计数,因为您只需要重复项。并且还将所有内容都放在一个新数组中,该数组还将 station
的数字传递到一个新数组中。
[
[
2,
{
"26": 2
}
],
...
]
从这里你知道站 2
记录了温度 26
2
次。您可以通过遍历结果将它们放在一个字符串中。
运行看下面的片段运行.
// Make sure that your temp recordings are in a single array with
// arrays inside of it.
// Each position (left -> right) marking the index of the station.
const times = [
[22, 26, 18, 12, 20, 17, 18, 19],
[20, 26, 15, 13, 20, 18, 18, 20],
[19, 28, 17, 15, 20, 16, 15, 18]
];
const getRepeatedTempsPerStation = times => {
// Get longest length of times array.
const length = Math.max(...times.map(time => time.length));
// Create a new array with the temps per station.
const tempsPerStation = [];
for (let i = 0; i < length; i++) {
tempsPerStation[i] = [];
for (let j = 0; j < times.length; j++) {
tempsPerStation[i].push(times[j][i]);
}
}
// Per station count how many times a single temp occurs.
const countsPerStation = tempsPerStation.map(temps =>
temps.reduce((acc, temp) => {
acc[temp] = (acc[temp] || 0) + 1;
return acc;
}, {})
);
// Give each set of counts the number of the station and
// filter out all stations without any duplicate numbers.
const stationsWithRepeatedTemps = countsPerStation
.map((counts, index) => [index + 1, Object.fromEntries(
Object.entries(counts).filter(([temp, count]) =>
count > 1 && !isNaN(temp))
)])
.filter(([station, counts]) => Object.keys(counts).length > 0);
return stationsWithRepeatedTemps;
}
// Get the result.
const results = getRepeatedTempsPerStation(times);
// Loop over the result and output it in a string.
for (const [station, tempCounts] of results) {
for (const [temp, count] of Object.entries(tempCounts)) {
console.log(`${temp} is repeated ${count} times in station #${station}`);
}
}
此代码段还会检查集合数组中的任何项目是否不是数字,因此如果某些数据未通过并且 null
已通过,则没有问题。
我有来自 8 个站点的实时温度日志用于科学研究。 每当所有8个站点都获取到数据时,将其推送到一个8列的数组中。
例如以下 table
Time1 22 26 18 12 20 15 .. for 8 stations
Time2 20 26 15 13 20 18...
Time3 19 28 17 15 20 16 ...
时间 1 是最近的数据标签...时间 2 是前一个,依此类推。
此处 26 在第 2 站重复 2 次
20 is repeated 3 times in station #5
所以预期的输出类似于上面的2句话。
我不是一个普通的(学过的).js 程序员,但可以在系统上调整主例程 运行 中的代码。
有没有办法用信号表示站点的温度重复以及重复多少次?
谢谢。
这是一个很好的算法,当然它可以更简单,但我是这样做的。
创建一个包含时间和温度的二维数组。父数组将为 times
,子数组将为 temperatures
每个 station
。每个子数组有 8 个条目。该子数组中的每个位置都表示 station
.
首先,您必须转换数据并为每个 station
.
temperatures
组
[
[22, 20, 19],
[26, 26, 28],
[18, 15, 17],
...
]
遍历新集合并计算每个站点数组中 temperatures
的重复项数。将每个站阵列放在一个对象中,您可以在其中计数。
[
{
"22": 1,
"20": 1,
"19": 1,
},
{
"26": 2,
"28": 1,
},
{
"18": 1,
"15": 1,
"17": 1,
},
...
]
现在过滤掉任何低于 1
的站点计数,因为您只需要重复项。并且还将所有内容都放在一个新数组中,该数组还将 station
的数字传递到一个新数组中。
[
[
2,
{
"26": 2
}
],
...
]
从这里你知道站 2
记录了温度 26
2
次。您可以通过遍历结果将它们放在一个字符串中。
运行看下面的片段运行.
// Make sure that your temp recordings are in a single array with
// arrays inside of it.
// Each position (left -> right) marking the index of the station.
const times = [
[22, 26, 18, 12, 20, 17, 18, 19],
[20, 26, 15, 13, 20, 18, 18, 20],
[19, 28, 17, 15, 20, 16, 15, 18]
];
const getRepeatedTempsPerStation = times => {
// Get longest length of times array.
const length = Math.max(...times.map(time => time.length));
// Create a new array with the temps per station.
const tempsPerStation = [];
for (let i = 0; i < length; i++) {
tempsPerStation[i] = [];
for (let j = 0; j < times.length; j++) {
tempsPerStation[i].push(times[j][i]);
}
}
// Per station count how many times a single temp occurs.
const countsPerStation = tempsPerStation.map(temps =>
temps.reduce((acc, temp) => {
acc[temp] = (acc[temp] || 0) + 1;
return acc;
}, {})
);
// Give each set of counts the number of the station and
// filter out all stations without any duplicate numbers.
const stationsWithRepeatedTemps = countsPerStation
.map((counts, index) => [index + 1, Object.fromEntries(
Object.entries(counts).filter(([temp, count]) =>
count > 1 && !isNaN(temp))
)])
.filter(([station, counts]) => Object.keys(counts).length > 0);
return stationsWithRepeatedTemps;
}
// Get the result.
const results = getRepeatedTempsPerStation(times);
// Loop over the result and output it in a string.
for (const [station, tempCounts] of results) {
for (const [temp, count] of Object.entries(tempCounts)) {
console.log(`${temp} is repeated ${count} times in station #${station}`);
}
}
此代码段还会检查集合数组中的任何项目是否不是数字,因此如果某些数据未通过并且 null
已通过,则没有问题。