2017-07-27 47 views
-1

我有一個系統,用於將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",]; 

我想一些幫助。

回答

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", ""] 

來源:https://www.w3schools.com/jsref/jsref_split.asp

3

需要分割字符串

links = files_list.split(',') 
相關問題