Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中将url文件放入数组列表_Javascript_Php_Mysql_Arrays_Ajax - Fatal编程技术网

如何在javascript中将url文件放入数组列表

如何在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]; 这显示了错误,因此我如何分离每个

我有一个系统,我在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];
这显示了错误,因此我如何分离每个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", ""]
资料来源: