1

I have an array as follows in javascript:

var linePoints = [[0, 3], [4, 8], [8, 5], [12, 13]];

But I would like to load this from a textfile instead of defining it in the code. The file:

input.txt:

0 3
4 8
8 5
12 13

I noticed this other post, which talks about something similar, but for a 1D array. I was wondering if there is a special function to do this for 2D. In java, I would read each line, then split it. But to be fair, I am new with javascript.

6
  • are you using node.js ? Commented Aug 25, 2016 at 21:43
  • Not that I know of, I am using it in jquery.flot Commented Aug 25, 2016 at 21:45
  • Do you control the format of the input file? Commented Aug 25, 2016 at 21:46
  • Yes, I could easily change it so each line is [0,3] for instance. Commented Aug 25, 2016 at 21:48
  • I guess in that case, if I prepend the datafile with <script>var linePoints = [ ... etc I could just load it that way. Slightly less pretty as I have different data files, but it could work. Commented Aug 25, 2016 at 21:50

3 Answers 3

4

This should do what you're looking for:

$.get("textFile.txt", function(data) {
      var items = data.split("\r\n").map(function(el){ return el.split(" ");});
  });

Here a working example: http://plnkr.co/edit/htsf9yBuygAhv7vznS2g?p=preview

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

14 Comments

Yes, one additional question. How do I make "items" available outside this function in javascript? It seems to be only in the jquery function. Sorry I'm new with jquery. Is there an easy way to "return" this variable?
I've tried the following to adapt the code to return data as a variable. function showGetResult (name, callback) { data= $.get(name, callback); items = data.split(/\r?\n/).map( pair => pair.split(/\s+/).map(Number) ); return items; } var items = showGetResult("./moonlight_sonata_diameter.data", function(data){ );
ok what we are dealing with is asynchronous code, you can deal with async code in few ways, the most common being callbacks and promises, there are other ways too but they don't work in all the browsers
what you're doing is almost correct but there's basically no way to make your various items contain the result in that way. what you can do instead is to write your logic inside the callback, I'll draft up an example in the next hour
I think the dependencies were the wrong way round, jquery needs to be loaded before jquery flot, I forked it here: plnkr.co/edit/PzU4yjzczBuWDGP5MaKL?p=preview
|
2

You need to pay attention to different newline styles, and you probably want the array to have numbers, not strings.

So I propose this code:

$.get("textFile.txt", function(data) {
    var items = data.split(/\r?\n/).map( pair => pair.split(/\s+/).map(Number) );
});

Comments

0

There's no built-in generalized function for parsing input the way you want. However you could take advantage of the String.split() function which will break a string into any number of smaller strings based on the delineator you pass it:

var foo = "0 3";
var bar = foo.split(" "); // split based on single space character
console.log(bar); // ["0", "3"]

1 Comment

What about the line breaks? Could I use this asychronously?

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.