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

我需要过滤时间戳为> 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

解决方案:

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

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>

给TA打赏
共{{data.count}}人
人已打赏
未分类

Windows应用程序在点击按钮时大部分时间都会冻结。

2022-9-8 19:59:38

未分类

有没有办法从Google TTS输出中删除padding?

2022-9-8 19:59:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索