1

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

后端完成了,我得到了这两个数组:

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

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]}

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

谢谢!

4

3 回答 3

3

如果你使用 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);

于 2020-06-22T09:11:32.803 回答
0

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));

于 2020-06-22T09:02:01.427 回答
0

我认为你可以这样做:

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]])

于 2020-06-22T09:52:19.100 回答