Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript循环,用于检测由至少三个相同数字组成的序列何时开始或结束_Javascript_Arrays_Function_Loops - Fatal编程技术网

Javascript循环,用于检测由至少三个相同数字组成的序列何时开始或结束

Javascript循环,用于检测由至少三个相同数字组成的序列何时开始或结束,javascript,arrays,function,loops,Javascript,Arrays,Function,Loops,我有一个如下所示的对象数组: let data = [ {timestamp: "2020-09-23T14:01:59.425Z", value: 5.12002}, {timestamp: "2020-09-24T20:02:20.656Z", value: 3.13285}, {timestamp: "2020-09-24T21:02:28.307Z", value: 2.84637}, {timestamp: &

我有一个如下所示的对象数组:

let data = [
  {timestamp: "2020-09-23T14:01:59.425Z", value: 5.12002},
  {timestamp: "2020-09-24T20:02:20.656Z", value: 3.13285},
  {timestamp: "2020-09-24T21:02:28.307Z", value: 2.84637},
  {timestamp: "2020-09-24T22:04:09.648Z", value: 3.57281},
  {timestamp: "2020-09-24T23:02:10.257Z", value: 10},
  {timestamp: "2020-09-25T04:00:54.857Z", value: 10},
  {timestamp: "2020-09-25T05:01:47.202Z", value: 10},
  {timestamp: "2020-09-25T06:02:27.773Z", value: 10},
  {timestamp: "2020-09-25T07:01:43.621Z", value: 10},
  {timestamp: "2020-09-25T08:01:07.918Z", value: 10},
  {timestamp: "2020-09-25T09:02:48.843Z", value: 5.2812},
  {timestamp: "2020-09-25T10:02:44.509Z", value: 10},
  {timestamp: "2020-09-25T11:01:47.002Z", value: 10},
  {timestamp: "2020-09-25T12:02:46.718Z", value: 10},
  {timestamp: "2020-09-25T13:00:32.164Z", value: 10},
  {timestamp: "2020-09-28T05:02:44.037Z", value: 10},
  {timestamp: "2020-09-28T14:01:57.837Z", value: 10},
  {timestamp: "2020-09-29T11:01:34.661Z", value: null},
  {timestamp: "2020-09-29T17:02:20.367Z", value: null},
  {timestamp: "2020-09-29T18:02:23.282Z", value: 8.49232},
  {timestamp: "2020-09-29T19:00:39.818Z", value: 7.38261},
  {timestamp: "2020-09-30T03:01:49.720Z", value: 6.28182},
  {timestamp: "2020-09-30T05:01:35.621Z", value: null},
  {timestamp: "2020-09-30T06:00:27.023Z", value: null},
  {timestamp: "2020-09-30T07:03:00.132Z", value: null}
];
我想创建一个函数,该函数在这个数组中循环,并返回所有结束一系列最小值为3的项({ 让结果=[]; 让计数=0; for(设i=0;i我不确定代码中的
段是从哪里来的,但下面是我很快想到的

jfdetection = () => {
  let result = [];
  // Keep track of consecutive high or low values
  let highCount = 1;
  let lowCount = 1;
  for (let i = 1; i < data.length; i++) {
    // Increment the counter if the previous value was similar, 
    // reset the counter if previous value was different.
    if (data[i].value === 10) {
      highCount = (data[i-1].value === 10) ? (highCount+1) : 1;
    } else if (data[i].value != 10) {
      lowCount = (data[i-1].value != 10) ? (lowCount+1) : 1;
    }

    // If we hit the required 3 similar values and there were 3 different values before, 
    // add to result
    if (highCount === 3 && lowCount >= 3) {
      result.push(data[i-2]);
    } else if (lowCount === 3 && highCount >= 3) {
      result.push(data[i-2]);
    }
  }
  return result;
}
jfdetection=()=>{
让结果=[];
//跟踪连续的高或低值
设highCount=1;
设lowCount=1;
for(设i=1;i=3){
结果推送(数据[i-2]);
}否则如果(低计数===3和高计数>=3){
结果推送(数据[i-2]);
}
}
返回结果;
}

我不确定代码中的
段是从哪里来的,但下面是我很快想到的

jfdetection = () => {
  let result = [];
  // Keep track of consecutive high or low values
  let highCount = 1;
  let lowCount = 1;
  for (let i = 1; i < data.length; i++) {
    // Increment the counter if the previous value was similar, 
    // reset the counter if previous value was different.
    if (data[i].value === 10) {
      highCount = (data[i-1].value === 10) ? (highCount+1) : 1;
    } else if (data[i].value != 10) {
      lowCount = (data[i-1].value != 10) ? (lowCount+1) : 1;
    }

    // If we hit the required 3 similar values and there were 3 different values before, 
    // add to result
    if (highCount === 3 && lowCount >= 3) {
      result.push(data[i-2]);
    } else if (lowCount === 3 && highCount >= 3) {
      result.push(data[i-2]);
    }
  }
  return result;
}
jfdetection=()=>{
让结果=[];
//跟踪连续的高或低值
设highCount=1;
设lowCount=1;
for(设i=1;i=3){
结果推送(数据[i-2]);
}否则如果(低计数===3和高计数>=3){
结果推送(数据[i-2]);
}
}
返回结果;
}

以下内容维护六槽参考框架,当它与所需模式之一匹配时,会将相关项目添加到结果中

const data=[{timestamp:“2020-09-23T14:01:59.425Z”,值:5.12002},{timestamp:“2020-09-24T20:02:20.656Z”,值:3.13285},{timestamp:“2020-09-24T21:02:28.307Z”,值:2.84637},{timestamp:“2020-09-24T22:04:09.648Z”,值:3.57281},{timestamp:“2020-09-24T23:02:10:257Z”,值:2554},值:10},{时间戳:“2020-09-25T05:01:47.202Z”,值:10},{时间戳:“2020-09-25T06:02:27.773Z”,值:10},{时间戳:“2020-09-25T07:01:43.621Z”,值:10},{时间戳:“2020-09-25T08:01:07.918Z”,值:10},{时间戳:“2020-09-25T09:02:48.843Z”,值:5.2812},{“2020-09-25T11:01:47.002Z”,值:10},{时间戳:“2020-09-25T12:02:46.718Z”,值:10},{时间戳:“2020-09-25T13:00:32.164Z”,值:10},{时间戳:“2020-09-28T05:02:44.037Z”,值:10},{时间戳:“2020-09-28014:01:57.837Z”,值:10},{时间戳:“2020-09-29T11:01:34.661Z”,值:367}“,值:null},{时间戳:“2020-09-29T18:02:23.282Z”,值:8.49232},{时间戳:“2020-09-29T19:00:39.818Z”,值:7.38261},{时间戳:“2020-09-30T03:01:49.720Z”,值:6.28182},{时间戳:“2020-09-30T05:01:35.621Z”,值:null},{时间戳:“2020-09-30T06:00:27.023Z”,值:null:13230Z},{
常量模式=/lllhh | hhll/
功能流程(数据){
常量结果=[]
常量帧=[]
for(设x=0;xconsole.log(process(data))
以下内容维护一个六槽参考框架,当它与所需模式之一匹配时,它会将相关项添加到结果中

const data=[{timestamp:“2020-09-23T14:01:59.425Z”,值:5.12002},{timestamp:“2020-09-24T20:02:20.656Z”,值:3.13285},{timestamp:“2020-09-24T21:02:28.307Z”,值:2.84637},{timestamp:“2020-09-24T22:04:09.648Z”,值:3.57281},{timestamp:“2020-09-24T23:02:10:257Z”,值:2554},值:10},{时间戳:“2020-09-25T05:01:47.202Z”,值:10},{时间戳:“2020-09-25T06:02:27.773Z”,值:10},{时间戳:“2020-09-25T07:01:43.621Z”,值:10},{时间戳:“2020-09-25T08:01:07.918Z”,值:10},{时间戳:“2020-09-25T09:02:48.843Z”,值:5.2812},{“2020-09-25T11:01:47.002Z”,值:10},{时间戳:“2020-09-25T12:02:46.718Z”,值:10},{时间戳:“2020-09-25T13:00:32.164Z”,值:10},{时间戳:“2020-09-28T05:02:44.037Z”,值:10},{时间戳:“2020-09-28014:01:57.837Z”,值:10},{时间戳:“2020-09-29T11:01:34.661Z”,值:367}“,值:null},{时间戳:“2020-09-29T18:02:23.282Z”,值:8.49232},{时间戳:“2020-09-29T19:00:39.818Z”,值:7.38261},{时间戳:“2020-09-30T03:01:49.720Z”,值:6.28182},{时间戳:“2020-09-30T05:01:35.621Z”,值:null},{时间戳:“2020-09-30T06:00:27.023Z”,值:null:13230Z},{
常量模式=/lllhh | hhll/
功能流程(数据){
常量结果=[]
常量帧=[]
for(设x=0;xjfdetection = () => {
  let result = [];
  // Keep track of consecutive high or low values
  let highCount = 1;
  let lowCount = 1;
  for (let i = 1; i < data.length; i++) {
    // Increment the counter if the previous value was similar, 
    // reset the counter if previous value was different.
    if (data[i].value === 10) {
      highCount = (data[i-1].value === 10) ? (highCount+1) : 1;
    } else if (data[i].value != 10) {
      lowCount = (data[i-1].value != 10) ? (lowCount+1) : 1;
    }

    // If we hit the required 3 similar values and there were 3 different values before, 
    // add to result
    if (highCount === 3 && lowCount >= 3) {
      result.push(data[i-2]);
    } else if (lowCount === 3 && highCount >= 3) {
      result.push(data[i-2]);
    }
  }
  return result;
}