Data erase in an array - javascript

I'm new in code with Javascript and I have a problem with an array I need to create.
var tabLine = new Array();
var tabCol = new Array();
var tabComplet = new Array();
var fso, f1, ts, s, cl, ln;
var ForReading = 1;
var i = 0;
function ReadFiles() {
fso = new ActiveXObject("Scripting.FileSystemObject");
// Read the contents of the file.
ts = fso.OpenTextFile("PathToAFile\TextFile.txt", ForReading);
s = ts.ReadAll();
tabLine = s.split('\n');
cl = tabLine.length;
ts.Close();
for (i = 0; i <tabLine.length; i++) {
var tabCol = tabLine[i].split("\t");
for (j=0;j<tabCol.length;j++) {
tabComplet[i,j] = tabCol[j];
alert(tabComplet[i,j]);
}
}
alert(tabComplet[10,5]);
alert(tabComplet[3,5]);
}
ReadFiles();
The file I need to read is a text file; It have many line and each line have information separate by tabulation.
This function read the text file and convert it into an array in two dimensions.
I had some alert to check the contain of my array.
The first alert give me the result I want (it display the content of the array witch is different for each element in the array)but the two other give me the same result. I check with another alert and, in the for boucle, these two element are different.
I make more test and all happen like if my array only have the same line copy/paste.
Thanks in advance for all information that can help me.
Here is an example of file I use :
http://www.k-upload.fr/afficher-fichier-2017-05-18-1b0cfa685testimport2.txt.html
Usually there are bigger than this one but for test it's OK.

Related

How to change JSON into string

i got a words array in JSON form like:
var words = [];
words.push({word:"I",x:50,y:50});
and i want it to store into a txt file which user can download from the website.
...
var data = JSON.stringify(words);
var file = new Blob([data],{type:'plain/text'});
...
however the output file is like this:
{"word":"Peaceful","x":40,"y":65,"lyric":"lyric"}
but i want it only print out the word "peaceful", i tried to use
var data = JSON.stringify(words.word);
but the output shows undefined.
words is an array, so you have to index it to access the object that you pushed onto it:
var data = JSON.stringify(words[0].word);
If I understand you correctly.
var myWords = [];
(for var i = 0; i < words.length; i++) {
var word = words[i].word;
myWords.push(word);
}
myWords.join(' '); // or myWords.join('\n');

JSON Data to Javascript Array Undefined

I am trying to get data from a JSON file and use Javascript Code to put it into Google Spreadsheet, I did good with some part of the data but I'm stuck with the other part, where I want to match the User Id and put all the data with it on a same row,
JSON Data:
"m":{"414":{"a":{"0":{"c":38,"p":12812.4},
"4":{"c":35,"p":10559.94},"2":{"c":43,"p":35811.63},
"6":{"c":48,"p":45530}},"d":{"0":{"c":55,"p":5477.06225},
"4":{"c":694,"p":106649.473},"2":{"c":1844,"p":716733.50775000011},
"6":{"c":605,"p":324152.5875}},"i":{"0":{"c":0,"p":0},
"4":{"c":0,"p":0},"2":{"c":0,"p":0},"6":{"c":542,"p":19893.93}}},
"404":{"a":{"0":{"c":15,"p":916.182},"4":{"c":50,"p":12357},
"2":{"c":530,"p":390825.27},"6":{"c":58,"p":4841.55}},
"d":{"0":{"c":10,"p":3145.8},"4":{"c":770,"p":141876.12},
"2":{"c":4854,"p":2173966.6125000003},
"6":{"c":1973,"p":1145077.425}},"i":{"0":{"c":0,"p":0},
"4":{"c":0,"p":0},"2":{"c":0,"p":0},"6":{"c":594,"p":25444.41}}}},
Javascript:
var testUF = [];
var Uid = Object.getOwnPropertyNames(doc1.m);
for (var lp2 = 0; lp2 < Uid.length; lp2++) {
var Ua1 = doc1.m[lp2].a["0"].p;
var Ua2 = doc1.m[lp2].a["4"].p;
var Ua3 = doc1.m[lp2].a["2"].p;
var Ua4 = doc1.m[lp2].a["6"].p;
var Ud1 = doc1.m[lp2].d["0"].p;
var Ud2 = doc1.m[lp2].d["4"].p;
var Ud3 = doc1.m[lp2].d["2"].p;
var Ud4 = doc1.m[lp2].d["6"].p;
var Ui4 = doc1.m[lp2].i["6"].p;
testUF.push([Uid,Ua1,Ua2,Ua3,Ua4,Ud1,Ud2,Ud3,Ud4,Ui4]);}
I am getting the Array on the Uid while Debugging, but all the other Variables don't get the data it stays Undefined. I want all the other variables to match with the Uid's and stay in the same row. I did the JSON parsing and everything.
I am asking for the first time on stackoverflow, please forgive me if I couldn't state everything properly. Thank you for the help. :)
You're not using the array index for Uid properly.
Change:
var Ua1 = doc1.m[lp2].a["0"].p;
To:
var Ua1 = doc1.m[Uid[lp2]].a["0"].p;

Read variables from a file in Javascript

I have a variable called words in Javascript like this:
var words = [{"text":"This", "url":"http://google.com/"},
{"text":"is", "url":"http://bing.com/"},
{"text":"some", "url":"http://somewhere.com/"},
{"text":"random", "url":"http://random.org/"},
{"text":"text", "url":"http://text.com/"},
{"text":"InCoMobi", "url":"http://incomobi.com/"},
{"text":"Yahoo", "url":"http://yahoo.com/"},
{"text":"Minutify", "url":"http://minutify.com/"}]
and I use the variable elements as for example words[0].url which points to the first url, i.e http://google.com/, etc.
If I store the data in a file like this (I call it file.csv):
This, http://google.com/
is, http://bing.com/
some, http://somewhere.com/
random, http://random.org/
text, http://text.com/
InCoMobi, http://incomobi.com/
Yahoo, http://yahoo.com/
Minutify, http://minutify.com/
How can I read the file in Javascrip and re-create variable words, with the exact same format as I mentioned earlier, i.e re-create:
var words = [{"text":"This", "url":"http://google.com/"},
{"text":"is", "url":"http://bing.com/"},
{"text":"some", "url":"http://somewhere.com/"},
{"text":"random", "url":"http://random.org/"},
{"text":"text", "url":"http://text.com/"},
{"text":"InCoMobi", "url":"http://incomobi.com/"},
{"text":"Yahoo", "url":"http://yahoo.com/"},
{"text":"Minutify", "url":"http://minutify.com/"}]
It looks like there are two steps. First is to get the external file, and the next step is to get it into a format you want it.
If you're not using jquery, first step is:
var file = new XMLHttpRequest();
file.onload = function() {
alert(file.responseText);
}
file.open('GET', 'file.csv');
file.send();
Next step is to take that file.responseText and format it. I might do:
var file = new XMLHttpRequest();
var words = [];
file.onload = function() {
var lines = file.responseText.split("\n");
for (var i = 0; i < lines.length; i++) {
var word = {};
var attributes = lines[i].split(",");
word.text = attributes[0];
word.url = attributes[1];
words.push(word);
}
}
file.open('GET', 'file.csv');
file.send();
If you're using a JSON file, just change the function above to be:
file.onload = function() {
words = JSON.parse(file.responseText);
}
Keep in mind that the words variable will not be available until the onload function runs, so you should probably send it to another function that uses it.
You could use the fetch API, it has many advantages and one of them is very short syntax, unlike the XMLHttpRequest constructor.
fetch("object.json").then(function(data){window.data=data.json()});
//then access the data via [window.data]

number of xml attributes->javascript multi array->function / process

I have comments in a database relative to each post. It pulls the post and all comments according to that post in one query, and groups them inside an XML node. I get the amount of attributes in each node, and take away the standard number of attributes that every post has by default, and that leaves me with the number of comments.
The comment structure is as follows:
comment0 Hey nice post!
commentdate0 2014-12-1 08:25:02
commentaudthor0 Chris
comment1 cool!
commentdate1 2014-08-2 09:25:02
commentaudthor1 Jason
and so on, the comments increase by that number.
So I need to check how many comments there are (done) and then retrieve them from the xml node (using $(this).attr('comment'+i)) Where i would be the counter (comment0, comment1 and so on)
Here is my current code to get it into the array:
var comms = new Array();
var count = this.attributes.length;
var av = count-11;
if(av != 0) {
for(var i=0; i<av; i++) {
for(var j=0; j<2; j++){
comms[i][j] = $(this).attr('comment'+i);
comms[i][j+1] = $(this).attr('commentdate'+i);
comms[i][j+2] = $(this).attr('commentauthor'+i);
}
}
}
But it is giving me the following error:
Uncaught TypeError: Cannot set property '0' of undefined
Now, how can I load it into a multi dimensional array to store the data, pass it to a function, and then process each row separately?
ie: this is what I am trying to do:
Array {
'comment1':
comment
commentdate
commentauthor
'comment2':
comment
commentdate
commentauthor
}
and then how would I be able to process each comment inside the function? ie: with each comment, do this.
Thanks in advance!
You need to create the inner array before adding to it. Try this:
var comms = new Array();
var count = this.attributes.length;
var av = count-11;
//if(av != 0) { // I commented this condition out, as it is not needed here
for(var i=0; i<av; i++) {
comms[i] = []; // Create a new array here before adding to it (this syntax is more common than the longer "new Array()" syntax you used
comms[i][0] = $(this).attr('comment'+i);
comms[i][1] = $(this).attr('commentdate'+i);
comms[i][2] = $(this).attr('commentauthor'+i);
}
//}

ActiveXObject("Scripting.FileSystemObject") not working for me

I want to add names of files of a specific folder to an JS's array, but nothing happens:
var pics = new Array();
var x;
var fs = new ActiveXObject("Scripting.FileSystemObject");
alert('x');
var fo = fs.GetFolder(Server.MapPath("C:\wamp\www\newsite\ErfanGhiasiPanel\Slider Images"));
for (x in fo.files){
pics.push(x.Name);
}
For instance, whet I insert an
alert('something')
after var fs = new ActiveXObject... or next lines, it won't appear.
What you guys think?
Thank you
Assuming JScript + Classic ASP due to the MapPath (which you dont need in your case) you need to escape the path string;
var pics = [];
var fs = new ActiveXObject("Scripting.FileSystemObject");
var fo = new Enumerator(fs.GetFolder("C:\\wamp\\www\\newsite\\ErfanGhiasiPanel\\Slider Images").Files);
for (; !fo.atEnd(); fo.moveNext()) {
pics.push(fo.item(0).Name)
}

Categories