Ad Code

Ticker

6/recent/ticker-posts

How to read text file into array in jquery


Jquery is a library of javascript which is work and implementation is very easy to compare to javascript. Jquery has many functions to operate easily like query selector of HTML dom and looping, filtering, ajax, and some other helpful functions.

Here we describe how to read the text files inside jquery and stores into the array.
File reading using get function which is based on ajax requests it gets the response of file content like if text then texts if other formats then it goes to the response.

Here I write code for reading file content into the jquery code.


$.get('file_to_read.txt', function(data) {
   //do_something_with data
}, 'text');
here $.get is jquery functions and file_to_read.txt is a file path which you want to read and function(data){} is a callback function which gives the response of reader contents after that I have passed datatype which you want to an inside response like TEXT, JSON, and HTML.

inside callback function response is given into the data variable which can you perform your won operation on the response contents.

File text have comma-separated to store in the array:-
If file content has comma-separated then you can store in the array using split method to make very easy comma-separated text to the array.
var txtarray = data.split(',');
File text read line-by-line into the array:-
this is same as splitting comma-separated function using comma it has to separate by new line like this
var txtarray = data.split("\n");
after that, you can use your array as you want to inside your project requirements.

Post a Comment

0 Comments

Ad Code