我正在尝试比较同一数组中的对象。我有一个CSV文件,该文件在定界列表中包含一些数据。提取文件后,我需要根据以下两个条件对其进行过滤并将输出打印到屏幕或控制台:
如果对于相同的SatelliteID,在五分钟的间隔内有三个读数低于红色下限。
如果对于同一颗卫星,有五个恒温器读数在五分钟的间隔内超过红色上限。
我相信我可以先使用array.filter然后再使用array.map来获得所需的输出,但不确定从哪里遍历数组并比较对象,该从哪里走。
这是期望的结果:
[
{
"satelliteId": 1000,
"severity": "RED HIGH",
"component": "TSTAT",
"timestamp": "2018-01-01T23:01:38.001Z"
},
{
"satelliteId": 1000,
"severity": "RED LOW",
"component": "BATT",
"timestamp": "2018-01-01T23:01:09.521Z"
}
]
CSV文件如下所示:
timestamp|satelliteid|redhigh|yellowhigh|yellowlow|redlow|rawvalue|component
20180101 23:01:05.001|1001|101|98|25|20|99.9|TSTAT
20180101 23:01:09.521|1000|17|15|9|8|7.8|BATT
20180101 23:01:26.011|1001|101|98|25|20|99.8|TSTAT
20180101 23:01:38.001|1000|101|98|25|20|102.9|TSTAT
20180101 23:01:49.021|1000|101|98|25|20|87.9|TSTAT
20180101 23:02:09.014|1001|101|98|25|20|89.3|TSTAT
20180101 23:02:10.021|1001|101|98|25|20|89.4|TSTAT
20180101 23:02:11.302|1000|17|15|9|8|7.7|BATT
20180101 23:03:03.008|1000|101|98|25|20|102.7|TSTAT
20180101 23:03:05.009|1000|101|98|25|20|101.2|TSTAT
20180101 23:04:06.017|1001|101|98|25|20|89.9|TSTAT
20180101 23:04:11.531|1000|17|15|9|8|7.9|BATT
20180101 23:05:05.021|1001|101|98|25|20|89.9|TSTAT
20180101 23:05:07.421|1001|17|15|9|8|7.9|BATT
这是我编写的用于提取文件并解析它的代码。
const readFile = require("fs").readFile;
readFile("./data.csv", "utf-8", (err, data) => {
//Store final results
var result = [];
//Split Each line in the CVS
const lines = data.split("\n");
//Assumes the first line in the csv is the header
var headers = lines[0].split("|");
//Create value pair
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split("|");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
//Add objects to result array
result.push(obj);
}
console.log(result);
});
到目前为止,这是解析CSV文件后记录到控制台的内容。
[
{
timestamp: '20180101 23:01:05.001',
satelliteid: '1001',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '99.9',
component: 'TSTAT'
},
{
timestamp: '20180101 23:01:09.521',
satelliteid: '1000',
redhigh: '17',
yellowhigh: '15',
yellowlow: '9',
redlow: '8',
rawvalue: '7.8',
component: 'BATT'
},
{
timestamp: '20180101 23:01:26.011',
satelliteid: '1001',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '99.8',
component: 'TSTAT'
},
{
timestamp: '20180101 23:01:38.001',
satelliteid: '1000',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '102.9',
component: 'TSTAT'
},
{
timestamp: '20180101 23:01:49.021',
satelliteid: '1000',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '87.9',
component: 'TSTAT'
},
{
timestamp: '20180101 23:02:09.014',
satelliteid: '1001',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '89.3',
component: 'TSTAT'
},
{
timestamp: '20180101 23:02:10.021',
satelliteid: '1001',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '89.4',
component: 'TSTAT'
},
{
timestamp: '20180101 23:02:11.302',
satelliteid: '1000',
redhigh: '17',
yellowhigh: '15',
yellowlow: '9',
redlow: '8',
rawvalue: '7.7',
component: 'BATT'
},
{
timestamp: '20180101 23:03:03.008',
satelliteid: '1000',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '102.7',
component: 'TSTAT'
},
{
timestamp: '20180101 23:03:05.009',
satelliteid: '1000',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '101.2',
component: 'TSTAT'
},
{
timestamp: '20180101 23:04:06.017',
satelliteid: '1001',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '89.9',
component: 'TSTAT'
},
{
timestamp: '20180101 23:04:11.531',
satelliteid: '1000',
redhigh: '17',
yellowhigh: '15',
yellowlow: '9',
redlow: '8',
rawvalue: '7.9',
component: 'BATT'
},
{
timestamp: '20180101 23:05:05.021',
satelliteid: '1001',
redhigh: '101',
yellowhigh: '98',
yellowlow: '25',
redlow: '20',
rawvalue: '89.9',
component: 'TSTAT'
},
{
timestamp: '20180101 23:05:07.421',
satelliteid: '1001',
redhigh: '17',
yellowhigh: '15',
yellowlow: '9',
redlow: '8',
rawvalue: '7.9',
component: 'BATT'
}
]
您可以reduce
一起使用filter
。
const arr = [ { timestamp: '20180101 23:01:05.001', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '99.9', component: 'TSTAT' }, { timestamp: '20180101 23:01:09.521', satelliteid: '1000', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.8', component: 'BATT' }, { timestamp: '20180101 23:01:26.011', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '99.8', component: 'TSTAT' }, { timestamp: '20180101 23:01:38.001', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '102.9', component: 'TSTAT' }, { timestamp: '20180101 23:01:49.021', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '87.9', component: 'TSTAT' }, { timestamp: '20180101 23:02:09.014', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.3', component: 'TSTAT' }, { timestamp: '20180101 23:02:10.021', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.4', component: 'TSTAT' }, { timestamp: '20180101 23:02:11.302', satelliteid: '1000', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.7', component: 'BATT' }, { timestamp: '20180101 23:03:03.008', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '102.7', component: 'TSTAT' }, { timestamp: '20180101 23:03:05.009', satelliteid: '1000', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '101.2', component: 'TSTAT' }, { timestamp: '20180101 23:04:06.017', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.9', component: 'TSTAT' }, { timestamp: '20180101 23:04:11.531', satelliteid: '1000', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.9', component: 'BATT' }, { timestamp: '20180101 23:05:05.021', satelliteid: '1001', redhigh: '101', yellowhigh: '98', yellowlow: '25', redlow: '20', rawvalue: '89.9', component: 'TSTAT' }, { timestamp: '20180101 23:05:07.421', satelliteid: '1001', redhigh: '17', yellowhigh: '15', yellowlow: '9', redlow: '8', rawvalue: '7.9', component: 'BATT' } ];
const res = arr.reduce((acc, {timestamp,satelliteid:satelliteId,redlow,redhigh,component})=>{
const getDate = str => new Date(str.slice(0,4)+'-'+str.slice(4,6)+'-'+str.slice(6));
const date = getDate(timestamp);
let severity;
if(arr.filter(obj=>+obj.rawvalue < +redlow && getDate(obj.timestamp) - date <= 5 * 60 * 1000).length >= 3){
severity = 'RED LOW';
} else if(arr.filter(obj=>+obj.rawvalue > +redhigh && getDate(obj.timestamp) - date <= 5 * 60 * 1000).length >= 3){
severity = 'RED HIGH';
}
if(severity){
acc.push({satelliteId,severity,component,timestamp});
}
return acc;
}, []);
console.log(res);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句