打印数组js只显示对象

时间:2016-09-20 11:48:31

标签: javascript arrays

我需要阅读大文本文件并查找其长度并保存数据。 我想将他们的内容保存为数组。

当我调试程序时,我可以看到数组不是空的,我可以看到想要的内容。

但是当我尝试打印数组时,我得到的是[object Object]。

代码

  function ReadAllFileFromFileList(files, allFileGenesDetails) {

    $("#my-progressbar-container").show();

    //Retrieve all the files from the FileList object
    if (files) {
      for (var i = 0, f; f = files[i]; i++) {
        var r = new FileReader();


        r.onload = (function(f) {
          var callBckFunction = RunVanDiagramAlgorithm_phase2;
          return function(e) {

            var fileGenesDetails = new Array();
            var geneQuery = new OrderedMap();

            var contents = e.target.result;

            // Parse the data
            var contentEachLine = contents.split("\n");
            for (var jj = 0; jj < contentEachLine.length; jj++) {
              var lineContent = contentEachLine[jj].split("\t");

              // Verify there line structure is correct
              if (lineContent.length >= 2) {
                var geneDetails = {
                  Query: lineContent[0],
                  Subject: lineContent[1]
                };

                if (!m_vennDiagramArguments.chkRemoveDuplicates_isChecked || !geneQuery.isContainKey(geneDetails.Query)) {
                  geneQuery.set(geneDetails.Query, geneDetails.Query);

                  fileGenesDetails.push(geneDetails);
                }
              }

            }
            // thats the array Im trying to print 

            allFileGenesDetails.push(fileGenesDetails);
            document.getElementById("resultss").innerHTML = allFileGenesDetails.toString();

            FinishReadingFile(callBckFunction);
          };
        })(f);

2 个答案:

答案 0 :(得分:1)

var fileGenesDetails = new Array();
...
allFileGenesDetails.push(fileGenesDetails);

您正在获取[object Object],因为您的数组包含另一个数组,而Arrays.prototype.toString()不会深入到多维数组中。

你应该迭代抛出allFileGenesDetails,例如

var str;
allFileGenesDetails.forEach(function(array){
     str += array.toString() + ";"; // do some formatting here
});

或者你想将allFileGenesDetails.push(fileGenesDetails)替换为更多代码,这些代码将一个数组中的所有项添加到另一个数组中。

答案 1 :(得分:0)

如果您直接尝试在打印方法中使用数组,您将获得&#34; Object object&#34;您必须使用

迭代所有值,将其解析为某种格式
var stringToShow;
allFileGenesDetails.forEach(function(itemInArray){
stringToShow+=itemInArray;// do something with the item here
});

或者如果你只是想看看阵列里面有什么console.log(JSON.stringify(allFileGenesDetails));