2017-07-27 12 views
-1

mysqlデータベースの各行にurlファイルを格納するシステムがあります。私はこのデータを配列に配置し、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]; 

これはエラーを示して、私はこのことから、各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",]; 

私はいくつかの助けをしたいと思います。

答えて

1

を分割する必要があります。 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

あなたはsplit()文字列関数を使用することができ、文字列に

links = files_list.split(',') 
関連する問題