-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.

0

2 Answers 2

3

you need to split the string

links = files_list.split(',')
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.