使用javascript过滤特定时间戳之间的数据。

问题描述 投票:0回答:1

我需要过滤时间戳为> 06的数据。

"use strict"

const data =  [ 
  {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224},
  {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436},
  {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804},
  {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941},
  {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049},
  {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846},
  {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298},
  {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267},
  {timestamp: "2020-04-23 05:45", totalAvg: 3.491275770565422},
  {timestamp: "2020-04-23 05:50", totalAvg: 3.9865817073170735},
  {timestamp: "2020-04-23 05:55", totalAvg: 2.8146341463414632},
  {timestamp: "2020-04-23 06:00", totalAvg: 4.0066161616161615},
  {timestamp: "2020-04-23 06:05", totalAvg: 4.870049261083743},
  {timestamp: "2020-04-23 06:10", totalAvg: 3.3189162561576357}
];


var filteredZoneData = _.filter(data, o => {
  return !((parseInt((o.timestamp).substring(11, 13)) < 06) || (parseInt(o.timestamp.substring(11, 13)) < 24));
})

console.log(filteredZoneData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

在这里,使用06会出现以下错误 Legacy octal literals are not allowed in strict mode

javascript lodash
1个回答
1
投票

你可以直接用想要的值作为字符串检查子串。

const data =  [ 
  {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224},
  {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436},
  {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804},
  {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941},
  {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049},
  {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846},
  {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298},
  {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267},
  {timestamp: "2020-04-23 05:45", totalAvg: 3.491275770565422},
  {timestamp: "2020-04-23 05:50", totalAvg: 3.9865817073170735},
  {timestamp: "2020-04-23 05:55", totalAvg: 2.8146341463414632},
  {timestamp: "2020-04-23 06:00", totalAvg: 4.0066161616161615},
  {timestamp: "2020-04-23 06:05", totalAvg: 4.870049261083743},
  {timestamp: "2020-04-23 06:10", totalAvg: 3.3189162561576357}
];


var filteredZoneData = _.filter(data, o => 
  o.timestamp.slice(11, 13) >= '06' && o.timestamp.slice(11, 13) < '24'
)

console.log(filteredZoneData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

1
投票

反正parseInt的时候不需要测试06,所以改成了

var filteredZoneData = _.filter(
  data, o => +o.timestamp.substring(11, 13) >= 6 // there will be no 24 in the timestamp 
); 

或一个范围。

var filteredZoneData = _.filter(data, o => { 
  const t = +o.timestamp.substring(11, 13);
  return t >= 6 && t < 24;
}); 

例子:使用 lodash

"use strict"

const data = [ {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224}, {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436}, {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804}, {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941}, {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049}, {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846}, {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298}, {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267}, {timestamp: "2020-04-23 05:45", totalAvg: 3.491275770565422}, {timestamp: "2020-04-23 05:50", totalAvg: 3.9865817073170735}, {timestamp: "2020-04-23 05:55", totalAvg: 2.8146341463414632}, {timestamp: "2020-04-23 06:00", totalAvg: 4.0066161616161615}, {timestamp: "2020-04-23 06:05", totalAvg: 4.870049261083743}, {timestamp: "2020-04-23 06:10", totalAvg: 3.3189162561576357} ];

var filteredZoneData = _.filter(data, o => +o.timestamp.substring(11, 13) >= 6); // there will be no 24 in the timestamp

console.log(filteredZoneData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

但JS可以不用库,为什么要用库呢?

const data = [ {timestamp: "2020-04-23 05:05", totalAvg: 2.596211180124224}, {timestamp: "2020-04-23 05:10", totalAvg: 3.22052273203436}, {timestamp: "2020-04-23 05:15", totalAvg: 4.75883386099804}, {timestamp: "2020-04-23 05:20", totalAvg: 4.052205882352941}, {timestamp: "2020-04-23 05:25", totalAvg: 6.801380301941049}, {timestamp: "2020-04-23 05:30", totalAvg: 5.147239169614846}, {timestamp: "2020-04-23 05:35", totalAvg: 5.035438241980298}, {timestamp: "2020-04-23 05:40", totalAvg: 5.043628013777267}, {timestamp: "2020-04-23 05:45", totalAvg: 3.491275770565422}, {timestamp: "2020-04-23 05:50", totalAvg: 3.9865817073170735}, {timestamp: "2020-04-23 05:55", totalAvg: 2.8146341463414632}, {timestamp: "2020-04-23 06:00", totalAvg: 4.0066161616161615}, {timestamp: "2020-04-23 06:05", totalAvg: 4.870049261083743}, {timestamp: "2020-04-23 06:10", totalAvg: 3.3189162561576357} ];


let filteredZoneData = data.filter(
  dt => dt.timestamp.split(" ")[1] > "06"
);
console.log(filteredZoneData);

// or if you prefer

filteredZoneData = data.filter(
  dt => parseInt(dt.timestamp.split(" ")[1]) >= 6
);

console.log(filteredZoneData);
© www.soinside.com 2019 - 2024. All rights reserved.