badbuda
badbuda

Reputation: 93

printing an array js shows only object

I need to read large text files and find their length and save their data. I want to save their content as an array.

when I debug the program I can see the array isn't empty, and I can see the wanted content.

But when I try to print the array all I get is [object Object].

Code

  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);

Upvotes: 0

Views: 1002

Answers (2)

ike3
ike3

Reputation: 1800

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

You are getting [object Object] because your array contains another array and Arrays.prototype.toString() does not go deep into multi-dimensional array.

You should iterate throw allFileGenesDetails such as

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

Or you want to replace allFileGenesDetails.push(fileGenesDetails) to some more code which adds all items from one array into another.

Upvotes: 1

Sujal Mandal
Sujal Mandal

Reputation: 1039

if you directly try to use a array in a print method you will get "Object object" you have to parse it into some format by iterating through all values using

var stringToShow;
allFileGenesDetails.forEach(function(itemInArray){
stringToShow+=itemInArray;// do something with the item here
});

or alternatively if you just want to see whats there inside the array do a console.log(JSON.stringify(allFileGenesDetails));

Upvotes: 0

Related Questions