如何使用Javascript将VTT文件读入数组和循环

如何使用Javascript将VTT文件读入数组和循环,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个带有JW播放器标题的VTT文件,我正在尝试创建一个交互式转录本。为此,我需要将VTT文件读入数组,然后与数据交互 以下是VTT文件的一个片段: 1 00:00:00 --> 00:00:05 65 MEP我们在屏幕上也有我们两人的推特手柄,所以如果你愿意的话 2. 00:00:05 --> 00:00:08 在事后与这些人互动,这是目前最好的方式 3. 00:00:08,051 --> 00:00:12,310 谈论一个在我心目中非常重要的话题,这是否产生了你想要的东西 var dat

我有一个带有JW播放器标题的VTT文件,我正在尝试创建一个交互式转录本。为此,我需要将VTT文件读入数组,然后与数据交互

以下是VTT文件的一个片段:

1
00:00:00 --> 00:00:05
65 MEP我们在屏幕上也有我们两人的推特手柄,所以如果你愿意的话
2.
00:00:05 --> 00:00:08
在事后与这些人互动,这是目前最好的方式
3.
00:00:08,051 --> 00:00:12,310

谈论一个在我心目中非常重要的话题,这是否产生了你想要的东西

var data = `1
00:00:00 --> 00:00:05
65 MEP we have twitter handles for both of us on screen as well so if you want

2
00:00:05 --> 00:00:08
to interact with those afterwards that's the best way to do so now been going to

3
00:00:08,051 --> 00:00:12,310
be talking about a topic that's extremely important topic in my mind and`;

data.split("\n\n").map(function (item) {
  var parts = item.split("\n");
  return {
    number: parts[0],
    time: parts[1],
    text: parts[2],
  };
});
以上内容将分组拆分为两个新行字符,然后再拆分为一个新行字符

其结果是:

[
  {
    "number": "1",
    "time": "00:00:00 --> 00:00:05",
    "text": "65 MEP we have twitter handles for both of us on screen as well so if you want"
  },
  {
    "number": "2",
    "time": "00:00:05 --> 00:00:08",
    "text": "to interact with those afterwards that's the best way to do so now been going to"
  },
  {
    "number": "3",
    "time": "00:00:08,051 --> 00:00:12,310",
    "text": "be talking about a topic that's extremely important topic in my mind and"
  }
]

这就是我最终的工作

$.get('http://dev.sharepoint-videos.com/test.vtt,函数(数据){
//将所有标题读入数组
var items=data.split('\n\r\n');
控制台日志(项目);
//循环浏览所有标题
$。每个(项目、功能(索引、值){
var item=items[index]。拆分('\n');
控制台日志(项目);
});

});谢谢你的回复。我正在尝试将每个标题(3行组)拆分为一个单独的索引。出于某种原因,它只输出:
code
[Object]0:Objectnumber:“1”text:“65 MEP我们在屏幕上也有我们两人的twitter句柄,所以如果您想要“time:”00:00-->00:00:05“proto:Objectlength:1_uproto:Array[0]如果将第一个代码块复制到控制台并运行它,会发生什么情况?可能是我的示例代码使用了
\n\n
\n
,而示例代码使用了另一个组合,可能是
\n\r
\n
。谢谢。我终于知道我的代码哪里出错了。谢谢你让我走上正确的道路。顺便说一句,这段代码只对这个特定的用例有用。VTT格式还有其他部分。某些vtt中有注释(以注释开头)。某些vtt中有区域。您可以在此处阅读标准: