how to create an array by reading text file in javascript

后端 未结 5 1085
日久生厌
日久生厌 2020-12-22 08:35

I have a text file with profanity word separated by comma (,). Now I have to create an array by reading this text file, each and every profanity word should be stored into t

5条回答
  •  醉梦人生
    2020-12-22 09:04

    Here is a jQuery + AJAX Solution that you can use

    //HTML
    
    
    
    //JavaScript/jQuery
    $(function () {
    
      //Stop caching if csv is likely to change often (change $.get to $.ajax)
      $.ajaxSetup({
         cache: false
      });
    
      $('#load').click(function () {
    
         $.get($('#file').val(), function (content) {
            var output = content.split(new RegExp(",|\r"))
                                .map(function (element) {
               //Do what ever tidy up here
               return $.trim(element).toLowerCase();
            });
            console.log(output);
         });
    
      });
    
    });
    

    Tested on CSV from http://en.wikipedia.org/wiki/Comma-separated_values

    You could hide the file input control by using something like this http://plugins.jquery.com/project/custom-file

提交回复
热议问题