0

I read a csv file using javascript code and i want to push it into a a 2D Array in order to do some processing

var dataArray = new Array;
        function parseCSV(dataArray) {
            //replace UNIX new lines
            data = data.replace(/\r\n/g, "\n");
            //replace MAC new lines
            data = data.replace(/\r/g, "\n");
            //split into rows
            var rows = data.split("\n");

            // loop through all rows
            for (var i = 0; i < rows.length; i++) {
                // this line helps to skip empty rows
                if (rows[i]) {
                    // our columns are separated by comma
                    var column = rows[i].split(",");


                    var date=column[0];
                    var value = column[4];

                    // create object which contains all these items:
                    var dataObject = {
                        date: date,
                        T4: value 
                    };  
                   dataArray.push(dataObject);
                }
            }   
        } 
        }

As a test, i try to read a cell content and display on the web page to verify that i read the file.

<script type="text/javascript">
       var x= dataArray[1][4];
        document.write(x);
    </script>

but it doesn't show anything. Can anyone Help??

4

2 回答 2

1
var dataArray = new Array;

var data = "Date,Description,Created By,Modified By,T4 Tag\n";
data += "20170424,This is record 1,user1,none,Just work please.\n";
data += "20170424,This is record 2,user2,none,I'm trying too.\n";

function parseCSV(dataArray) {
    //replace UNIX new lines
    data = data.replace(/\r\n/g, "\n");
    //replace MAC new lines
    data = data.replace(/\r/g, "\n");
    //split into rows
    var rows = data.split("\n");

    // loop through all rows
    for (var i = 0; i < rows.length; i++) {
        // this line helps to skip empty rows
        if (rows[i]) {
            // our columns are separated by comma
            var column = rows[i].split(",");


            var date=column[0];
            var value = column[4];

            // create object which contains all these items:
            var dataObject = {
                date: date,
                T4: value 
            };  
           dataArray.push(dataObject);
        }
    }   
} 
parseCSV(dataArray);

由于缺少文件,我将一些 CSV 数据强制转换为字符串进行处理。您的问题没有显示这两项:

  1. 你是如何设置var 数据的
  2. 您如何调用parseCSV(dataArray)

如果这两件事都存在,那么一切都应该起作用 -这是证明


至于 html 脚本部分:

您的数组寻址看起来错误 - 看起来应该是这样的:

<script>
  var x = dataArray[1].T4;
  document.write(x);
</script>

这是我的推理

我了解您正在将 CSV 数据传递给此函数以将其解析为您的 dataArray 变量 - 对吗?

如果正确,这些数据看起来就像我手动添加的(我直接添加它只是为了处理某些东西)。

更大的项目不是数据来自哪里,而是最终由这个 parseCSV 函数解析。

发生这种情况时,您将通过以下方式构建附加到空的dataArray变量:

对于第一遍,这将如下所示:

[{“日期”:“日期”,“T4”:“T4标签”}]

对于第二遍:

[{“date”:“日期”,“T4”:“T4标签”},{“date”:“20170424”,“T4”:“请工作。”}]

第三遍:

[{“date”:“日期”,“T4”:“T4标签”},{“date”:“20170424”,“T4”:“请工作。”},{“date”:“20170424”, "T4" : "我也在尝试。"}]

所以现在当你在你的 html代码部分,您没有地址[4]的键。

你的代码说:

var x = dataArray[1][4];

请考虑以下内容以了解我的意思:

var record = dataArray[1];
//{"date" : "20170424", "T4" : "Just work please."}
//There is no key 4 here only date & T4.
var date = record.date;//returns: "20170424"
var t4 = record.T4;//returns: "Just work please."

相比之下,如果您有以下情况-您的方法将起作用

myObj = {"date" : "20170424", "4" : "This is a value for field 4", "T4" : "Just work please."}
myObj[4];//returns "This is a value for field 4";

希望这足以清楚地解释它。

于 2017-04-24T17:31:54.440 回答
0

这是加载csv文件功能

function loadCSV(file) {
            if (window.XMLHttpRequest) {
                // IE7+, Firefox, Chrome, Opera, Safari
                var request = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                var request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            // load
            request.open('GET', file, false);
            request.send();
            parseCSV(dataArray);
        }
于 2017-04-24T17:41:08.210 回答