【问题标题】:Find values in array depending on a pattern in Javascript根据 Javascript 中的模式在数组中查找值
【发布时间】:2018-05-24 11:41:44
【问题描述】:

我有一个数组:

var videoSources = ["0000.mp4", "0015.mp4", "0030.mp4", "0045.mp4", "0100.mp4"];

我会根据当前时间播放视频; mp4 每 15 分钟录制一次;

if time = 0044 play 0030.mp4

所以我需要像[0-15] 这样的模式来查找和播放正确的视频。 此处播放视频的功能:

var currentIndex = 0;
// listener function changes src
function myNewSrc() {
    var myVideo = document.getElementsByTagName('video')[0];
    myVideo.src = videoSources[currentIndex];
    myVideo.load();
}

【问题讨论】:

    标签: javascript arrays if-statement find pattern-matching


    【解决方案1】:

    我建议将文件名解析为整数,然后查找分钟标记是否在文件范围内:

    var videoSources = 
      ["0000.mp4", 
      "0015.mp4", 
      "0030.mp4", 
      "0045.mp4", 
      "0100.mp4"]
    
    var getTimestamp = (ts, sources) =>
      sources.find(name => {
        const startStr = name.split(".")[0];      // e.g.: "0015"
        const startMin = parseInt(startStr, 10);  // e.g.: 15
        const endMin = startMin + 15;
        
        return ts >= startMin && ts < endMin; 
      });
    
    console.log(getTimestamp(44, videoSources));

    如果不能保证范围为 15 分钟,您可以“向前看”以找到范围的 结束。在我当前的示例中,我将其硬编码为 15 分钟,并期望文件名是文件的 start

    编辑:支持小时和分钟

    var videoSources = 
      ["0000.mp4", 
      "0015.mp4", 
      "0030.mp4", 
      "0045.mp4", 
      "0100.mp4",
      "1145.mp4",
      "1230.mp4",
      "1245.mp4"]
    
    var getTimestamp = (h, m, sources) =>
      sources
        .sort()
        .find(name => {
          const hours = parseInt(name.slice(0, 2), 10);
          const minutesStart = parseInt(name.slice(2, 4), 10);
          const minutesEnd = minutesStart + 15;
          
          return h <= hours && m >= minutesStart && m < minutesEnd; 
        });
    
    console.log(getTimestamp(12, 44, videoSources));

    【讨论】:

    • 它可以工作,但我还需要解析小时,因为它会记录一整天。所以我会有:'code` var videoSources = [“0000.mp4”,“0015.mp4”,“0030.mp4”,“0045.mp4”,“0100.mp4”,“0115.mp4”,“0130” .mp4”、“0145.mp4”、“0200.mp4”、“0215.mp4”、“0230.mp4”、“0245.mp4”、“0300.mp4”、“0315.mp4”、“0330.mp4” ”、“0345.mp4”、“0400.mp4”、“0415.mp4”、“0430.mp4”、“0445.mp4”、“0500.mp4”、“0515.mp4”、“0530.mp4”、等等……
    • 如果格式始终为HHmm,您可以使用slice(0, 2)slice(2, 4) 分别获取小时和分钟。我将在我的答案中添加一个示例。
    • 您的代码运行良好!但是我将当前时间以这种格式 1230 存储到“时间”变量中,我在下面尝试了这个但没有成功:function addZero(i) { if (i &lt; 10) { i = "0" + i; } return i;} function myFunction() { var d = new Date(); etc... var time = Number(strtime); var getTimestamp = (ts, sources) =&gt; sources.find(name =&gt; { const startStr = name.split(".")[0]; const startMin = parseInt(startStr, 10); const endMin = startMin + 15; return ts &gt;= startMin &amp;&amp; ts &lt; endMin; });} document.write(getTimestamp(time, videoSources));
    【解决方案2】:

    您可以将时间数组和时间映射到数字,然后使用findIndex 方法从数组中返回正确时间元素的索引。

    var videoSources = ["0000.mp4", "0015.mp4", "0030.mp4", "0045.mp4", "0100.mp4"]
    
    const getIndex = (arr, time) => {
      time = parseInt(time);
      arr = arr.map(e => parseInt(e.split('.')[0]));
      return arr.findIndex((e, i) => time >= e && !arr[i + 1] || time < arr[i + 1]);
    }
    
    console.log(getIndex(videoSources, "0044"))
    console.log(getIndex(videoSources, "0015"))
    console.log(getIndex(videoSources, "0099"))
    console.log(getIndex(videoSources, "0120"))

    【讨论】:

    • 对不起,我不明白你的逻辑
    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 2023-03-02
    • 2023-03-28
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多