how to load data from text file to javascript array?

前端 未结 3 1104
失恋的感觉
失恋的感觉 2020-12-10 20:49

i have an array called items=[\"apple\",\"mango\",\"cherry\"];

i wonder how i can load the array data from text file instead of declaring it?the text f

3条回答
  •  死守一世寂寞
    2020-12-10 21:45

    With jQuery you can do something like this

      $.get("textFile.txt", function(data) {
          var items = data.split(',');
      });
    

    You may need something like this though

    var items = data.replace(/"/g, '').split(',');
    

    This is a start.

    If this is a user input file then you may need to upload it before you work with it.

提交回复
热议问题