0

Hello expert Please Help..

So I have 2 problem to get some array from raw file

First on html when try get array from js using getData function always return not define

Second Get array from txt file that only contain raw data no function at all

index.html

<script>
  var myData = []
  myData.push({id: 999,name: "Zee"}); //just for add some data
  //myData = getData(); << error not defined
  console.log(myData);
</script>

util.js

function getData() {
  array = [{id: 1,name: 'Alpha'}, 
           {id: 2,name: 'Beta'}]
  /*
  array = some file
  this file only contain this
  {id:1,name:'Alpha'},
  {id:2,name:'Beta'}
  */
  return array;
}

data.txt

 {id:1,name:'Alpha'},
 {id:2,name:'Beta'}

How to make this happen..

Ikuto Tohoin
  • 39
  • 1
  • 5

2 Answers2

1

If your html file, link script file which includes getData() function.

<script src="filename.js"></script>
<script>
  var myData = getData();
  myData.push({id: 999,name: "Zee"});
  console.log(myData);
</script>

For second question, you may want to take a look at the following question. Answers are already there.

How to read an external local JSON file in Javascript

Khay
  • 1,530
  • 13
  • 28
  • ok thanks.. I got wrong on src="filename.js" how about the 2nd question? – Ikuto Tohoin Aug 24 '18 at 03:58
  • You would want to save the data in a `.json` file and get it with `fetch()`. (I'm not showing you how, just giving some clues xD) – mauroc8 Aug 24 '18 at 04:25
  • If you're just starting to learn javascript, I'd say you avoid facing that problem for now (since it is a little complicated for beginners). – mauroc8 Aug 24 '18 at 04:28
  • Thanks @Khay, but more I read id more confuse I am.. I wish I can make some like `array = readfile(data.txt)` maybe you can help here [Code](https://plnkr.co/edit/7mxyFd2ad9U2s8T4x82N?p=preview). – Ikuto Tohoin Aug 24 '18 at 07:09
  • @mauroc8 yeah im just learn java and I want to learn the basic, avoid template or plugin stuff as possible if can I don't want to change anything on data.txt moreover change the file type – Ikuto Tohoin Aug 24 '18 at 07:22
  • @IkutoTohoin Java and JavaScript are two different languages. – Khay Aug 24 '18 at 07:35
  • I try learning .js one, why everyone make this so hard? can you just complete [This Code](https://plnkr.co/edit/7mxyFd2ad9U2s8T4x82N?p=preview) change the `array = readfile(data.txt)` into something simple – Ikuto Tohoin Aug 24 '18 at 09:55
  • No. There is no "simple" way of reading a file, because it has to be done asynchronously. I recommend you changing `data.txt` to `data.js`, and add it to the html: `` right before your other script. – mauroc8 Aug 24 '18 at 22:08
  • And you'd also have to change the contents of `data.js` to: `var data = [ {...}, {...} ]`. Then you'd have `data` as a global variable. – mauroc8 Aug 24 '18 at 22:08
  • @mauroc8 that data.txt is access from util.js not from html.. and consider that data.txt is read only.. no change at all and khay already told me about using src on html so I can access function on util.js – Ikuto Tohoin Aug 27 '18 at 03:12
0

This issue has been solved at 24 August using code below

Util.js

function readTextFile(file) {
  var allText;
  var rawFile = new XMLHttpRequest();
  rawFile.open("GET", file, false);
  rawFile.onreadystatechange = function() {
    if (rawFile.readyState === 4) {
      if (rawFile.status === 200 || rawFile.status == 0) {
        allText = rawFile.responseText;
      }
    }
  };
  rawFile.send(null);
  return allText;
}

function getData() {
  var str = "[" + readTextFile("data.txt") + "]";
  var array1 = [str];
  var array2 = JSON.parse(array1);
  return array2;
}

index.html

<script src="util.js">
  var myData = []
  myData = getData();
  console.log(myData);
</script>
Ikuto Tohoin
  • 39
  • 1
  • 5