我有一个系统,我将url文件存储在mysql数据库的每一行中。我想使用这些数据将它放入一个数组并在javascript函数中使用它。例如:
var files_list = "https://example.com/file1.docx,https://example.com/file2.docx,https://example.com/file3.docx,"; //value obtained via ajax
var links = [files_list];
这显示错误,因此我如何分隔每个网址并从中获取:
var links = ["https://example.com/file1.docx,https://example.com/file2.docx,https://example.com/file3.docx,"];
对此:
var links = ["https://example.com/file1.docx","https://example.com/file2.docx","https://example.com/file3.docx",];
我想要一些帮助。
答案 0 :(得分:3)
你需要拆分字符串
links = files_list.split(',')
答案 1 :(得分:1)
您可以使用split()
字符串函数。类似于files_list.split(",")
。
split()方法用于将字符串拆分为子字符串数组,并返回新数组。
示例:
var files_list = "https://example.com/file1.docx,https://example.com/file2.docx,https://example.com/file3.docx,"; //value obtained via ajax
var links = files_list.split(",");
console.log(links); // Will print this ["https://example.com/file1.docx", "https://example.com/file2.docx", "https://example.com/file3.docx", ""]