Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find max value of element in array located in text file 1

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
US
I am trying to find the max value of id:x in an associative array that is located in a text file located in the same folder as my script.

I found 2 working examples, one finds the max value and the other reads the array from a text file. I am trying to put them together, but the output is NaN (Not a Number). I am not sure what is wrong with the script.

Script that reads in the array
JavaScript:
 function readTextFile(file)
  {
      var rawFile = new XMLHttpRequest();
      rawFile.open("GET", file, true);
      rawFile.onreadystatechange = function ()
      {
          if(rawFile.readyState === 4)
         {
              if(rawFile.status === 200 || rawFile.status == 0)
              {
                  var data = rawFile.responseText;}
                  alert(data);                          
         }
      }
      rawFile.send(null); 
  }
readTextFile("test_array.txt");

Script that finds max value
JavaScript:
data = [
        {
        "id":1,
        "meetdate":"20180830"
        },
        {
        "id":2,
        "meetdate":"20180920"
        },
        { 
        "id":3,
        "meetdate":"20181025"
        }
        ]

    var max = Math.max.apply(null,
    Object.keys(data).map(function(e) {
        return data[e]['id'];
    }));
    var newid = max +1;
        alert(newid);

Script combined.
JavaScript:
function readTextFile(file)
  {
      var rawFile = new XMLHttpRequest();
      rawFile.open("GET", file, true);
      rawFile.onreadystatechange = function ()
      {
          if(rawFile.readyState === 4)
         {
              if(rawFile.status === 200 || rawFile.status == 0)
              {
                  var data = rawFile.responseText;}
                  //alert(data);
                   var max = Math.max.apply(null,
                   Object.keys(data).map(function(e) {
                       return data[e]['id'];
                  }));
                  var newid = max +1;
                  alert(newid); //Output desired                                    
         }
      }
      rawFile.send(null); 
  }
readTextFile("test_array.txt");

I am new to javascript, so I am open to other methods to accomplished the same task.
 
Hi

The [tt].responseText[/tt] property's value is string. Depending on what that string contains, [tt]JSON.parse()[/tt] may be a solution to convert it to array. Try :
Code:
[b]var[/b] data [teal]=[/teal] JSON[teal].[/teal][COLOR=orange]parse[/color][teal]([/teal]rawFile[teal].[/teal]responseText[teal]);[/teal]


Feherke.
feherke.github.io
 
Feherke,
Worked perfectly. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top