如何通过阅读文本文件中的JavaScript来创建一个数组 [英] how to create an array by reading text file in javascript

查看:133
本文介绍了如何通过阅读文本文件中的JavaScript来创建一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用逗号(,)分隔亵渎字的文本文件。现在我通过阅读这个文本文件中创建一个数组,每个亵渎字应该存储到阵列中。如果有人知道如何做到这一点,请尽快回复我越好。 注:我一定要存放在这个文件中的任何地方这是我的选择。我唯一​​要做的就是,通过只给文件的完整路径读取文本文件。这是没有必要的解决办法应该是在JavaScript中,你可以使用jQuery或Ajax解决方案答复。 谢谢你。

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 the array. if someone know how to do this please reply me as soon as possible. Note : I have to stored this file anywhere it's my choice. The only thing I have to do is, read the text file by just giving complete path of file. It is not necessary the solution should be in javascript you can reply with jquery or ajax solution. thank you.

推荐答案

下面是一个jQuery + Ajax解决方案,你可以使用

Here is a jQuery + AJAX Solution that you can use

//HTML
<input id="file" type="file" />
<input type="button" id="load" value="Load CSV" />

//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);
     });

  });

});

在测试了CSV从 http://en.wikipedia.org/wiki/Comma-separated_values​​

您可以隐藏通过使用这样的事情 HTTP文件输入控制://plugins.jquery。 COM /项目/自定义文件

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

这篇关于如何通过阅读文本文件中的JavaScript来创建一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆