Javascript 将json解析为js中的.csv

Javascript 将json解析为js中的.csv,javascript,jquery,json,csv,Javascript,Jquery,Json,Csv,我发现这个脚本(运行良好)除了我想跳过几列(我只想提取第1、2、3、9列)之外,所以我有: $(document).ready(function(){ $('button').click(function(){ var data = $('#txt').val(); if(data == '') return; JSONToCSVConvertor(data, "Agenda", true); }); }); function

我发现这个脚本(运行良好)除了我想跳过几列(我只想提取第1、2、3、9列)之外,所以我有:

$(document).ready(function(){
    $('button').click(function(){
        var data = $('#txt').val();
        if(data == '') return;

        JSONToCSVConvertor(data, "Agenda", true);
    });
});

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof JSONData != 'object'
      ? JSON.parse(JSONData)
      : JSONData;

    var CSV = '';
    //Set Report title in first row or line
    CSV += ReportTitle + '\r\n\n';



    //This condition will generate the Label/Header
    if (ShowLabel) {
      var row = "";

      //This loop will extract the label from 1st index of on array
      for (var index = 0; i<arrData.length-1; index++) {
        if (index  == 4 || index == 5 || index == 6 || index == 7 || index == 8) {
          continue;
        }
        else{
          //Now convert each value to string and comma-seprated
          row += index + ';';

        row = row.slice(0, -1);
        //append Label row with line break
        CSV += row + '\r\n';
        }
    }
    //1st loop is to extract each row
    for (var i = 0; i < arrData.length-1; i++) {
      if (i  == 4 || i == 5 || i == 6 || i == 7 || i == 8) {
        continue;
      }
      else{
        var row = "";
      }
      //2nd loop will extract each column and convert it in string comma-seprated
      for (var j = 0; i<arrData.length-1; j++) {
          if (j  == 4 || j == 5 || j == 6 || j == 7 || j == 8) {
            continue;
          }
          else{
            row += '"' + arrData[i][j] + '";';
          }
        }
      }

      row.slice(0, row.length - 1);

      //add a line break after each row
      CSV += row + '\r\n';
    }



    if (CSV == '') {
      alert("Invalid data");
      return;
    }

    //Generate a file name
    var fileName = "Formapelec_";
    //this will remove the blank-spaces from the title and replace it with an underscore
    fileName += ReportTitle.replace(/ /g,"_");

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

    // Now the little tricky part.
    // you can use either>> window.open(uri);
    // but this will not work in some browsers
    // or you will not get the correct file extension

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
var data=$('#txt').val();
如果(数据=“”)返回;
JSONToCSVConvertor(数据,“议程”,真实);
});
});
函数JSONToCSVConvertor(JSONData、ReportTitle、ShowLabel){
//如果JSONData不是对象,那么JSON.parse将解析对象中的JSON字符串
var arrData=typeof JSONData!=“对象”
?JSON.parse(JSONData)
:JSONData;
var CSV=“”;
//在第一行或第一行设置报表标题
CSV+=ReportTitle+'\r\n\n';
//此条件将生成标签/标题
如果(显示标签){
var行=”;
//此循环将从数组的第一个索引中提取标签
for(var index=0;i window.open(uri);
//但这在某些浏览器中不起作用
//否则您将无法获得正确的文件扩展名
//这个技巧将生成一个临时标记
var link=document.createElement(“a”);
link.href=uri;
//将可见性设置为隐藏,使其不会影响web布局
link.style=“可见性:隐藏”;
link.download=fileName+“.csv”;
//此部件将附加定位标记,并在自动单击后将其删除
document.body.appendChild(链接);
link.click();
document.body.removeChild(link);
}
它应该只使用列1、2、3和9生成一个.csv,但没有任何返回。我使用if(){continue}尝试了不同的方法,但我要么使用所有列返回,要么根本没有返回。

//第一个循环是提取每一行
//1st loop is to extract each row
for (var i = 0; i < arrData.length-1; i++) {
  if (i  == 4 || i == 5 || i == 6 || i == 7 || i == 8) {
    continue;
  }
对于(变量i=0;i
您也在跳过行,如果(var i=0;i不应该在那里{
for (var i = 0; i < arrData.length-1; i++) {
      //2nd loop will extract each column and convert it in string comma-seprated
      for (var j = 0; j<arrData[i].length; j++) {
          if (j  == 4 || j == 5 || j == 6 || j == 7 || j == 8) {
            continue;
          }
          else{
            row += '"' + arrData[i][j] + '";';
          }
        }
      }

      row.slice(0, row.length - 1);

      //add a line break after each row
      CSV += row + '\r\n';
    }
//第二个循环将提取每个列,并将其转换为逗号分隔的字符串
对于(var j=0;jj这里的问题是什么?请编辑代码以在问题中提供测试数据字符串。(逻辑应将其解析为对象)。如果没有足够的细节来重现或测试问题,则可能会被视为离题。有关提问的帮助,请查看。