Read in a .txt file

607
6
08-09-2017 03:51 PM
GregSalvador
New Contributor III

Hi all,

Basic question here, but how do I read in a .txt file and store it as an array? I am using javascript and arcgis' 4.4 API

I have several layers on a map that I want "visible = true" on startup. Instead of hard coding the titles of the layers in a giant if() statement, I would like to read in the titles from a text file. I think a simple array would be best to save the read in data. Once read in, I will loop through the array and access the desired layerViews that way.

What I'm currently doing:

if (item.title === "City Limits" || item.title === "Land" ||

item.title === "Parcels" || item.title === "Boundary" ||

item.title === "Fill" || item.title === "Other Cities & Towns Boundaries" || item.title === "Other Cities & Towns Fills") {

item.visible = true;

What I would Like to Do:

for(i = 0; i < dataArray.length; i++){

if(item.title === dataArray)

item.visible = true;

}

 

I have found reference material that mentions javascript's Node.js File System Module, so if that could be incorporated into my main .js script I would be a happy camper.

0 Kudos
6 Replies
FC_Basson
MVP Regular Contributor

You can add your layer names as an additional Javascript file to your app with all the layer names stored in a JSON object or array.  So in your HTML you add (in the header <head>):

<script src="showlayers.js"></script>

and the showlayers.js file content would be something like:

var visibleItems = [
   "City Limits",
   "Land",
   "Parcels",
   ...
];

And when checking for the layers to make visible:

for(i = 0; i < myItems.length-1; i++){
   var item = myItems[i];
   if(visibleItems.indexOf(item.title)  !=  -1){
      item.visible = true;
   }
}‍‍‍‍‍
ThomasSolow
Regular Contributor

I think this is the best solution.

You should note that the node fs module doesn't exist in browser javascript.  Browser javascript can't directly interact with the local filesystem.

GregSalvador
New Contributor III

Very interesting! So how does javascript write cookies to the local computer? As far as I know, cookies are written by javascript but stored on the local user to be retrieved and read later

0 Kudos
ThomasSolow
Regular Contributor

Good question.  I don't know many of the details here but the browser must have access to the filesystem at some level.  For instance, modern browsers include a database called indexedDB, which ultimately sits in the local filesystem (cookies, like you mentioned, and local storage must also sit in the local filesystem).  You can write to and read from indexedDB from JavaScript, but the browser mediates this; there's no direct access from JavaScript to the filesystem.

It does seem like it would be possible to write your list of items to indexedDB (or local storage, or a cookie) from inside the browser and subsequently pull the list from those sources (or write it out, if it doesn't exist).  On some level you are interacting with the local filesystem when you do this, but not directly.

If you want to directly access files directly you have three options as far as I know: files must be dragged and dropped into the browser using the drag and drop API, or selected by a user using the file input, or retrieved from an http server using an http request.

My understanding is that node's fs module is just a nice JavaScript wrapper around your operating system's syscalls to read/write files, it doesn't actually represent a native JavaScript way to read/write files.

0 Kudos
GregSalvador
New Contributor III

Thank you very much, this is exactly what I need! I've never had anyone reply to me on internet forums, this is very exciting lol

FC_Basson
MVP Regular Contributor

Welcome to the Geonet community! 

0 Kudos