如何将url文件放入javascript中的数组列表中
问题描述:
我有一个系统,用于将url文件存储在mysql数据库的每一行中。我想使用这些数据将其放入一个数组中,并在javascript函数中使用它。例如:如何将url文件放入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",];
我想一些帮助。
答
可以使用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", ""]
答
需要分割字符串
links = files_list.split(',')