检查值是否在CSV文件中

时间:2018-05-09 10:14:20

标签: javascript jquery csv

我想要筛选csv文件并检查其中一列中是否存在短语(Pipe)。此时警告框弹出,表示存在所有不同版本的变量代码,这是不正确的。

处理CSV文件

的脚本
$(document).ready(function () {
    console.log('Enterre');
    $('#load_data').click(function () {
        $.ajax({
            type: "GET",
            url: "assets/exports/pipe.csv",
            success: function (data) {
                console.log("success");
                var code = "Test";
                // Remove \n and split by ,
                var pipeList = data.split('\n').map(function (row) {
                    return row.split(',')
                });
                // Array of arrays like be generated
                for (var i = 0; i < pipeList.length; i++) {
                    if ($.inArray(code, pipeList[i]) != -1) {
                        alert('value is in Array!');
                    }
                    else {
                        alert('value is not');
                    }
                }
            }
        });
    });
});

CSV档案

CSV file

1 个答案:

答案 0 :(得分:1)

看看这个改变的代码。

&#13;
&#13;
$(document).ready(function() {
  console.log('Enterre');
  $('#load_data').click(function() {

    $.ajax({
      type: "GET",
      url: "assets/exports/pipe.csv",

      success: function(data) {

        var code = "Pipe"; 
        // Flag that value found
        var found = false;
        // Remove \n and split by ,
        var pipeList = data.split('\n').map(function(row){return row.split(',')});
        // Array of arrays like be generated
          for(var i = 0; i < pipeList.length; i++){
              if ($.inArray(code, pipeList[i]) != -1) {          
                  found = true;
                  alert('value is Array!');
              }
          }
          if(!found){
            alert('value is not in Array');
          }
      }
    });
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="load_data">Load</button>
&#13;
&#13;
&#13;