如何在javascript中将url文件放入数组列表
我有一个系统,我在mysql数据库的每一行中存储url文件。我想使用这些数据将其放入数组并在javascript函数中使用。例如:如何在javascript中将url文件放入数组列表,javascript,php,mysql,arrays,ajax,Javascript,Php,Mysql,Arrays,Ajax,我有一个系统,我在mysql数据库的每一行中存储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 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];
这显示了错误,因此我如何分离每个url并从中获取:
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",];
我需要一些帮助。您需要拆分字符串
links = files_list.split(',')
您可以使用split()
string函数。类似于文件\u 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", ""]
资料来源: