-1

I have a system where I store url files in each row in my mysql database. I want to use this data to put it into an array and use it in a javascript function. For example:

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];

This shows error, so how can I separate each url and get from this:

var links = ["https://example.com/file1.docx,https://example.com/file2.docx,https://example.com/file3.docx,"];

To this:

var links = ["https://example.com/file1.docx","https://example.com/file2.docx","https://example.com/file3.docx",];

I would like some help.

NekoLopez
  • 579
  • 1
  • 9
  • 28

2 Answers2

3

you need to split the string

links = files_list.split(',')
serdar.sanri
  • 2,217
  • 1
  • 17
  • 21
1

You can use split() string function. Something like files_list.split(",").

The split() method is used to split a string into an array of substrings, and returns the new array.

Example:

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

Source: https://www.w3schools.com/jsref/jsref_split.asp