检查值是否在CSV文件中

问题描述 投票:0回答:1

我想要筛选一个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

javascript jquery csv
1个回答
1
投票

看看这个改变的代码。

$(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');
          }
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="load_data">Load</button>
© www.soinside.com 2019 - 2024. All rights reserved.