【问题标题】:Return a word from a words array when the time is the one from the time array. Javascript - React当时间是时间数组中的时间时,从单词数组中返回一个单词。 Javascript - 反应
【发布时间】:2020-06-22 08:57:10
【问题描述】:

我正在使用 React 构建音频或视频文件的转录服务。

后端已经完成,我得到了这 2 个数组:

我有一个时间数组,以秒为单位:

const timing = [0, 0.2, 0.8, 0.9, 1.3, 2]

我还有一个单词数组:

const words = ["hello", "world", "I", "am", "John", "Smith"]

我只想在时机合适的时候突出显示一个词。

除了“if”语句的无穷大之外,还有其他方法吗?

if(time = timing[0]) {return words[0]}
if(time = timing[1]) {return words[1]}
...
if(time = timing[n]) {return words[n]}

(*时间等于视频/音频播放器)

谢谢!

【问题讨论】:

  • 所以timing数组中的项目应该等于words数组?

标签: javascript arrays reactjs timing transcription


【解决方案1】:

如果你使用 dict 和 setInterval 会更容易

const words = {
    0: "hello",
  200: "world",
  800: "I",
  900: "am",
  1300: "John",
  2000: "Smith"
}

function speak(timing) {
    if(timing in words) {
    console.log(words[timing])
  }
}

current_timestamp = 0
function timer() {
    speak(current_timestamp)
  current_timestamp += 100
}
setInterval(timer, 100);

【讨论】:

    【解决方案2】:

    const timing = [0, 0.2, 0.8, 0.9, 1.3, 2];
    const words = ['hello', 'world', 'I', 'am', 'John', 'Smith'];
    
    function getWord(timing, words, time) {
        return words[timing.indexOf(time)];
    }
    
    console.log(getWord(timing, words, 0.9));

    【讨论】:

      【解决方案3】:

      我认为你可以这样做:

      const timing = [0, 0.2, 0.8, 0.9, 1.3, 2]
      
      const words = ["hello", "world", "I", "am", "John", "Smith"];
      var hashTable = {}
      timing.forEach((i, index)=>{
        hashTable[i] = words[index];
      });
      
      console.log(hashTable)
      
         console.log(hashTable[timing[3]])

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 2017-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多