【问题标题】:How to put url files into an array list in javascript [duplicate]如何将url文件放入javascript中的数组列表中[重复]
【发布时间】:2018-01-03 13:33:18
【问题描述】:

我有一个系统,我在我的 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",];

我需要一些帮助。

【问题讨论】:

    标签: javascript php mysql arrays ajax


    【解决方案1】:

    你需要拆分字符串

    links = files_list.split(',')
    

    【讨论】:

      【解决方案2】:

      您可以使用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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-02
        • 2023-03-23
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 2014-11-18
        相关资源
        最近更新 更多