0

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);
1
  • try console.log(JSON.stringify(array)). Commented Sep 20, 2016 at 11:52

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

thank you so much! it works now Im trying to save the array as cvs file ive tried to use this code - jsfiddle.net/cr4gL29v and just switch the var to (JSON.stringify(allFileGenesDetails)); but it doesnt work... any ideas why?
Array.toString() does it, because your array is a array of arrays just iterate over each array item & use the .toString() method on them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.