Elements will not add to Javascript array - javascript

I'm trying to load in two txt files and compare the differences between the two. More specifically I'm looping through one file per line and comparing it to every line in another txt file.
For the most part everything is working but I have found that I'm only able to access the array within the lr.on('line') function. However I have declared the array in the global scope.
Here is the code:
var LineByLineReader = require('line-by-line');
var lr = new LineByLineReader('phones.txt');
var lr2 = new LineByLineReader('wor.txt');
var phoneArray = [];
var worArray = [];
lr.on('error', function(err){
if(err){
console.log("We have found the following error: " + err);
}
});
lr2.on('error', function(err){
if(err){
console.log("We have found the following error: " + err);
}
});
lr.on('line', function(line){
phoneArray.push(line);
});
lr2.on('line', function(line){
worArray.push(line);
});
for(var i = 0; i < phoneArray.length; i++){
for(var x = 0; x < worArray.length; x++){
if(array1[i] === array2[x]){
console.log("Found Match: " + array2[x]);
}
}
}

Maybe you just forgot to use the right variables names inside your for loop?
And you just need this? :
if(phoneArray[i] === worArray[x]){
console.log("Found Match: " + worArray[x]);

Related

how to remove first 3 word in the sentence In javascript

How can I remove the first three words in java script when the response from the api I want to delete or remove the first three words.. the message from api is like below
"Error: GraphQL error: Account code already taken "
var temp = e.message
var temp1 = temp.map(function(f) {
return f.substring(temp.indexOf(' ') + 2);
});
console.log("Output", temp1)
expected output : " Account code already taken"
var st = "Error: GraphQL error: Account code already taken."
var s = st.split(' ')
s.splice(0,3)
st = s.join(' ');
console.log("Output", st)
Try this code
If the certain word like error: is present in the message then you can split the string with the word and return string from specific index:
var temp = "Error: GraphQL error: Account code already taken "
var temp1 = function(f) {
if(f.split('error:').length > 1)
return f.split('error:')[1].trim();
else return f;
};
console.log("Output:", temp1(temp))
Update: First, I will suggest you to fix the message format if possible. If you have to possible situation where the word in the message could be either error or error: then you can try the following:
var temp = "Error: GraphQL error Account code already taken "
var temp1 = function(f) {
var splitVal = 'error';
if(f.includes('error:'))
splitVal = 'error:';
if(f.split(splitVal).length > 1)
return f.split(splitVal)[1].trim();
else return f;
};
console.log("Output:", temp1(temp));
Create/modify the function to remove one word:
function removeOne(f) {
return f.substring(f.indexOf(' ') + 1);
}
and call it 3 times:
var temp1 = removeOne(removeOne(removeOne(temp)));
or in a loop:
var temp1 = temp;
for(var i = 0; i < 3; i++) {
temp1 = removeOne(temp1);
}
EDIT: wrapped the post into a snippet if someone had doubts:
var temp = "Error: GraphQL error: Account code already taken ";
/* Create/modify the function to remove one word: */
function removeOne(f) {
return f.substring(f.indexOf(' ') + 1);
}
/*and call it 3 times:*/
var temp1 = removeOne(removeOne(removeOne(temp)));
console.log("Output", temp1);
/*or in a loop:*/
temp1 = temp;
for(var i = 0; i < 3; i++) {
temp1 = removeOne(temp1);
}
console.log("Output", temp1);
const shorten = () => input.slice(21, input.length)

Adding Multiple Files into a Zip File not Working

I'm using JSZip to add selected files into a single zip file.
The issue is only the last selected file is being added to the zip (which is also corrupted), see code below:
var zip = new JSZip();
var items = '';
var count = 0;
var zipName = 'resources.zip';
$$('input[type=checkbox]').each(function(e){
if(e.checked){
if(e.id!='select-all'){
items = items + "'" + e.getAttribute('data-href') + "'|" + e.getAttribute('data-file') + ",";
}
}
});
if(items!=''){
items = items.slice(0,-1)
var tmp = items.split(',');
for(i=0;i<tmp.length;i++){
var item = tmp[i].split('|');
var url = item[0];
var filename = item[1];
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
throw err; // or handle the error
}
zip.file(filename, data, {binary:true});
count++;
if(count == tmp.length) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, zipName);
});
}
});
}
}
Can anyone see the issue with my code?
Fixed it. Issue was I wasn't putting the urls into an array correctly.
Corrected line:
items.push(e.getAttribute('data-href') + '|' + e.getAttribute('data-file'));

Javascript call by reference not working

I read this and tried implementing my function so that data doesn't change back, but it isn't working with me.
I have an array of objects, where I send them one by one to another function, to add data.
queries.first(finalObject.sectionProjects[i]);
for each one of the sectionProjects, there is a variable achievements, with an empty array.
Upon sending each sectionProject to the queries.first function, I reassign achievements,
finalObject.sectionProjects[i].achievements = something else
When I return from the queries.first function, I lose the data I added.
Am I doing something wrong?
Here's the function:
module.exports = {
first:function(aProject) {
// Latest achievements
var query =
" SELECT ta.description, ta.remarks, ta.expectedECD " +
" FROM project pr, task ta, milestone mi " +
" WHERE pr.ID = mi.project_ID AND mi.ID = ta.milestone_ID " +
" AND ta.achived = ta.percent AND pr.ID = " + aProject.project_id +
" ORDER BY pr.expectedECD " +
" LIMIT 5;"
;
var stringified = null;
pmdb.getConnection(function(err, connection){
connection.query(query, function(err, rows){
if(err) {
throw err;
}else{
var jsonRows = [];
for( var i in rows) {
stringified = JSON.stringify(rows[i]);
jsonRows.push(JSON.parse(stringified));
}
connection.release();
aProject.achievements = jsonRows;
upcomingTasks(aProject);
}
});
});
}
}
This is pmdb.js:
var mysql = require("mysql");
var con = mysql.createPool({
host: "localhost",
user: "user",
password: "password",
database: "database"
});
module.exports = con;
This is the main function that calls queries.first:
// ...Code...
//Number of section projects
var len = jsonRows.length;
console.log("Number of section projects: " + len);
var internal_counter = 0;
function callbackFun(i){
(finalObject.sectionProjects[i]).achievements = [];
queries.first(finalObject.sectionProjects[i]);
if(++internal_counter === len) {
response.json(finalObject);
}
}
var funcs = [];
for (var i = 0; i < len; i++) {
funcs[i] = callbackFun.bind(this, i);
}
for (var j = 0; j < len; j++) {
funcs[j]();
}
Read That Answer twice. Objects acts as a wrapper for the scalar primitive property. You are passing the Objects in to the "queries.first" function.
See this Object reference issue
Edited for the sample code
pmdb.getConnection(function(err, connection){
connection.query(query, function(err, rows){
if(err) {
throw err;
}else{
var jsonRows = [];
for( var i in rows) {
stringified = JSON.stringify(rows[i]);
jsonRows.push(JSON.parse(stringified));
}
connection.release();
aProject.achievements = jsonRows;
upcomingTasks(aProject)
}
});
});
that is not a problem. change it like this. "upcomingTasks" is not a callback function. it is execute after assign the achievements in aProject

For loop going out of range? [duplicate]

This question already has answers here:
What is an off-by-one error and how do I fix it?
(6 answers)
Closed 4 months ago.
I am using a library in Javascript to pull in data from a chat server. The library is irrelevant but is quickblox. The data comes back into my app and can be seen, but when I try an loop on an object they return, it goes out of range, can't get 'last_message' of undefined. Loop should run for res.items[i].length which at the mo is two, but it is trying to carry on running, it seems.
var onDialogs = function(err, res){
console.log("------------------------------------List Of Dialogs------------------------------------",res);
var count = 0;
var sent;
var i = 0;
console.log("res.items.length",res.items.length)
for (i;i<=res.items.length;i++){
console.log("this one: ", res.items[i]);
if (res.items[i].last_message===null||res.items[i].last_message===undefined||res.items[i].last_message===""){
alert("SOMETHING WENT WRONG");
}
else{
console.log("RES.ITEMS: ", res.items);
console.log("RES ITEMS LEN", res.items.length);
console.log("***********************I IS SET TO: ",i," *******************************************");
console.log("RAWR",res.items);
console.log(res.items[i].last_message_date_sent,res.items[i].last_message);
console.log(res.items[i]);
if (res.items[i].last_message === undefined || res.items[i].last_message===null || res.items[i].last_message===""){
console.log("FAIL");
}
else{
var dialogID = res.items[i]._id;
var sent = res.items[i].created_at;
console.log(res.items[i].created_at);
var year = sent.substring(0,4);
var month = sent.substring(5,7);
var day = sent.substring(8,10);
var userIDInChat;
var j =0;
userArray=[];
var userArray = res.items[i].occupants_ids;
console.log("USER ARRAY: ",userArray);
for (j; j<userArray.length; j++){
console.log(userArray[j]);
var testID = window.localStorage.getItem("userID");
console.log("USERARRAY[j]", userArray[j]);
if (userArray[j] != testID){
console.log("INSIDE THE IF STATEMENT");
userIDInChat = userArray[j];
window.localStorage.setItem("userIDInChat", userIDInChat);
console.log("//*******BLOCK ID SET TO********\\", userIDInChat, testID, res);
$.get("http://www.domain.co.uk/API/getUserByID.php", { userID: userIDInChat}, function (data) {
console.log("API CALL:",data);
chatOpponent = data;
console.log(chatOpponent);
console.log("------------------------------------------------------------------------------------------");
renderBlock(res,j,i,chatOpponent,userIDInChat,userArray,testID,day,month,year,dialogID);
});
}
}
}
}
}
//End block
};
function renderBlock(res,j,i,chatOpponent,userIDInChat,userArray,testID,day,month,year,dialogID){
console.log("(res,j,i,chatOpponent,userIDInChat,userArray,testID)");
console.log("RENDERBLOCK PARAMS: ",res,j,i,chatOpponent,userIDInChat,userArray,testID);
//insert function here
console.log("RES: ",res);
var senderID = userIDInChat;
//compare date - vs. moment - today, yesterday or date
sent = day + "/" + month + "/" + year;
console.log(sent);
var onMessages = function(err,result){
window.localStorage.setItem("RESULTTEST",result);
console.log("ONMESSAGESRESULTHERE",err,result);
//console.log("---------onMessages---------",result.items[i].date_sent);s
};
var msgList = QB.chat.message.list({chat_dialog_id: dialogID}, onMessages);
var messages;
console.log(messages);
if (res.items[i].last_message.length>=140) {
var last_message = res.items[i].last_message.substring(0,140)+".....";
}
else{
var last_message = res.items[i].last_message;
}
var readFlag = res.items[i].read;
console.log("SENDERID:", senderID, "username: ", chatOpponent, "last_message", last_message, "sentFlag");
if (readFlag === 1){
var read = "fa-envelope-o";
}
else {
var read = "fa-envelope";
}
var html = "<div class='messageBlock' id='"+senderID+"'><div style='width:10%;min-height:64px;float:left;'><i class='fa '"+read+"'></i><p>"+sent+"</p></div><div style='width:90%;min-height:64px;float:right;'><p class='user'><b><i>"+chatOpponent+"</b></i></p><p>"+last_message+"</p></div></div>";
Object being looped on:
Object {total_entries: 2, skip: 0, limit: 50, items: Array[2]}items: Array[2]0: Object_id: "54e4bd3929108282d4072a37"created_at: "2015-02-18T16:26:33Z"last_message: "test"last_message_date_sent: 1425640757last_message_user_id: 2351789name: nulloccupants_ids: Array[2]photo: nulltype: 3unread_messages_count: 0user_id: 2351781xmpp_room_jid: null__proto__: Object1: Object_id: "54ec990f29108282d40b19e2"created_at: "2015-02-24T15:30:23Z"last_message: "herro!"last_message_date_sent: 1424858692last_message_user_id: 2394026name: nulloccupants_ids: Array[2]photo: nulltype: 3unread_messages_count: 0user_id: 2351789xmpp_room_jid: null__proto__: Objectlength: 2__proto__: Array[0]limit: 50skip: 0total_entries: 2__proto__: Object
for (i;i<=res.items.length;i++){
should be
for (i;i<res.items.length;i++){
Simple you are looping one too many times.
for (i;i<=res.items.length;i++){
^^
arrays are zero index so that means that last index is the length minus one.
for (i;i<res.items.length;i++){
^^

Wait for Javascript Web Scraping Function to finish before running for next page?

I am attempting to create a web scraper (in node.js) that will pull down information from a site, and write it to a file. I have it built to correctly work for one page, but when I try to use the function in a for loop, to iterate through multiple games, I get bad data in all of the games.
I understand that this is related to Javascript's asynchronous nature, and I have read about callback functions, but I'm not sure I understand how to apply it to my code. Any help would be GREATLY appreciated:
for(x = 4648; x < 4650; x++){ //iterate over a few gameIDs, used in URL for request
scrapeGame(x);
}
function scrapeGame(gameId){
//request from URL, scrape HTML to arrays as necessary
//write final array to file
}
Essentially, what I am looking to do, is within the for loop, tell it to WAIT to finish the scrapeGame(x) function before incrementing x and running it for the next game -- otherwise, the arrays start to overwrite each other and the data becomes a huge mess.
EDIT: I've now included the full code which I am attempting to run! I'm getting errors when looking in the files after they are written. For example, the first file is 8kb, second is ~16, 3rd is ~32, etc. It seems things aren't getting cleared before running the next game.
Idea of the program is to pull Jeopardy questions/answers from the archive site in order to eventually build a quiz app for myself.
//Iterate over arbitrary number of games, scrape each
for(x = 4648; x < 4650; x++){
scrapeGame(x, function(scrapeResult) {
if(scrapeResult){
console.log('Scrape Successful');
} else {
console.log('Scrape ERROR');
}
});
}
function scrapeGame(gameId, callback){
var request = require('request');
cheerio = require('cheerio');
fs = require('fs');
categories = [];
categorylist = [];
ids = [];
clues = [];
values = ['0','$200','$400','$600','$800','$1000','$400','$800','$1200','$1600','$2000'];
valuelist = [];
answers = [];
array = [];
file = [];
status = false;
var showGameURL = 'http://www.j-archive.com/showgame.php?game_id=' + gameId;
var showAnswerURL = 'http://www.j-archive.com/showgameresponses.php?game_id=' + gameId;
request(showGameURL, function(err, resp, body){
if(!err && resp.statusCode === 200){
var $ = cheerio.load(body);
//add a row to categories to avoid starting at 0
categories.push('Category List');
//pull all categories to use for later
$('td.category_name').each(function(){
var category = $(this).text();
categories.push(category);
});
//pull all clue IDs (coordinates), store to 1d array
//pull any id that has "stuck" in the string, to prevent duplicates
$("[id*='stuck']").each(function(){
var id = $(this).attr('id');
id = id.toString();
id = id.substring(0, id.length - 6);
ids.push(id);
//if single J, pick category 1-6
if (id.indexOf("_J_") !== -1){
var catid = id.charAt(7);
categorylist.push(categories[catid]);
var valId = id.charAt(9);
valuelist.push(values[valId]);
}
//if double J, pick category 7-12
else if (id.indexOf("_DJ_") !== -1){
var catid = parseInt(id.charAt(8)) + 6;
categorylist.push(categories[catid]);
var valId = parseInt(id.charAt(10)) + 5;
valuelist.push(values[valId]);
}
//if final J, pick category 13
else {
categorylist.push(categories[13]);
}
});
//pull all clue texts, store to 1d array
$('td.clue_text').each(function(){
var clue = $(this).text();
clues.push(clue);
});
//push pulled values to big array
array.push(ids);
array.push(categorylist);
array.push(valuelist);
array.push(clues);
//new request to different URL to pull responses
request(showAnswerURL, function(err, resp, body){
if(!err && resp.statusCode === 200){
var $ = cheerio.load(body);
$('.correct_response').each(function(){
var answer = $(this).text();
answers.push(answer);
});
//push answers to big array
array.push(answers);
//combine arrays into 1-d array to prep for writing to file
for(var i = 0; i < array[0].length; i++){
var print = array[0][i] + "|" + array[1][i] + "|" + array[2][i] + "|" + array[3][i] + "|" + array[4][i];
var stringPrint = print.toString();
file.push(stringPrint);
}
//update string, add newlines, etc.
var stringFile = JSON.stringify(file);
stringFile = stringFile.split('\\').join('');
stringFile = stringFile.split('","').join('\n');
//write to file, eventually will append to end of one big file
fs.writeFile('J_GAME_' + gameId +'.txt', stringFile, function(err) {
if(err) {
console.log(err);
} else {
console.log("Game #" + gameId + " has been scraped.");
status = true;
}
});
}
});
}
});
//clear arrays used
valuelist = [];
answers = [];
categories = [];
categorylist = [];
ids = [];
clues = [];
array = [];
file = [];
//feed callback status
callback(status);
}
// Iterate over a few gameIDs, used in URL for request.
for (x = 4648; x < 4650; x++) {
// Pass in the callback as an anonymous function.
// So below I am passing in the id and the function I want to execute.
// AND, defining the results I am expecting as passed in arguments.
scrapeGame(x, function(scrapeResult, err) {
// This will *NOT* execute *UNTIL* you call it in the function below.
// That means that the for loop's execution is halted.
// This function receives the status that is passed in,
// in this case, a boolean true/false and an error if any.
if (scrapeResult) {
// Scrape was true, nothing to do.
// The for loop will now move on to the next iteration.
console.log('Scrape Successful');
} else {
// Scrape was false, output error to console.log and
// break loop to handle error.
console.log('Scrape ERROR :: ' + err);
// Notice we are calling break while in the
// scope of the callback function
// Remove the break if you want to just move onto
// the next game ID and not stop the loop
break;
}
});
}
// This function now accepts two arguments.
function scrapeGame(gameId, callback) {
// ************************************************
// ** Do Your Work Here **
// Request from URL, scrape HTML to arrays as necessary.
// Write final array to file.
// After file creation, execute the callback and pass bool
// status (true/false).
// ************************************************
var request = require('request'),
cheerio = require('cheerio'),
fs = require('fs'),
categories = [],
categorylist = [],
ids = [],
clues = [],
values = [
'0',
'$200',
'$400',
'$600',
'$800',
'$1000',
'$400',
'$800',
'$1200',
'$1600',
'$2000'
],
valuelist = [],
answers = [],
array = [],
file = [],
showGameURL = 'http://www.j-archive.com/showgame.php?game_id=' + gameId,
showAnswerURL = 'http://www.j-archive.com/showgameresponses.php?game_id=' + gameId;
request(showGameURL, function(err, resp, body) {
if (!err && resp.statusCode === 200) {
var $ = cheerio.load(body);
//add a row to categories to avoid starting at 0
categories.push('Category List');
//pull all categories to use for later
$('td.category_name').each(function() {
var category = $(this).text();
categories.push(category);
});
//pull all clue IDs (coordinates), store to 1d array
//pull any id that has "stuck" in the string, to prevent duplicates
$("[id*='stuck']").each(function() {
var id = $(this).attr('id');
id = id.toString();
id = id.substring(0, id.length - 6);
ids.push(id);
//if single J, pick category 1-6
if (id.indexOf("_J_") !== -1) {
var catid = id.charAt(7);
categorylist.push(categories[catid]);
var valId = id.charAt(9);
valuelist.push(values[valId]);
}
//if double J, pick category 7-12
else if (id.indexOf("_DJ_") !== -1) {
var catid = parseInt(id.charAt(8)) + 6;
categorylist.push(categories[catid]);
var valId = parseInt(id.charAt(10)) + 5;
valuelist.push(values[valId]);
}
//if final J, pick category 13
else {
categorylist.push(categories[13]);
}
});
//pull all clue texts, store to 1d array
$('td.clue_text').each(function() {
var clue = $(this).text();
clues.push(clue);
});
//push pulled values to big array
array.push(ids);
array.push(categorylist);
array.push(valuelist);
array.push(clues);
//new request to different URL to pull responses
request(showAnswerURL, function(err, resp, body) {
if (!err && resp.statusCode === 200) {
var $ = cheerio.load(body);
$('.correct_response').each(function() {
var answer = $(this).text();
answers.push(answer);
});
//push answers to big array
array.push(answers);
//combine arrays into 1-d array to prep for writing to file
for (var i = 0; i < array[0].length; i++) {
var print = array[0][i] + "|" + array[1][i] + "|" + array[2][i] + "|" + array[3][i] + "|" + array[4][i];
var stringPrint = print.toString();
file.push(stringPrint);
}
//update string, add newlines, etc.
var stringFile = JSON.stringify(file);
stringFile = stringFile.split('\\').join('');
stringFile = stringFile.split('","').join('\n');
//write to file, eventually will append to end of one big file
fs.writeFile('J_GAME_' + gameId + '.txt', stringFile, function(err) {
//clear arrays used
valuelist = [];
answers = [];
categories = [];
categorylist = [];
ids = [];
clues = [];
array = [];
file = [];
if (err) {
// ******************************************
// Callback false with error.
callback(false, err);
// ******************************************
} else {
console.log("Game #" + gameId + " has been scraped.");
// ******************************************
// Callback true with no error.
callback(true);
// ******************************************
}
});
}
});
}
});
}
My assumption is that you want them to be scraped one after one, not in parallel. So, for loop won't help. The following approach should do the trick:
var x = 4648;
var myFunc = scrapeGame(x, function cb(){
if(x >= 4650){
return;
}
x++;
return myFunc(x, cb);
});
function scrapeGame(gameId){
//request from URL, scrape HTML to arrays as necessary
//write final array to file
}
For nested async function, where you want them be executed in serial manner, you should just forget about for loop.
An example of correct request handling with http client:
function scrapeGame(gameId, cb){
//your code and set options
http.request(options, function(response){
var result = "";
response.on('data', function (chunk) {
result += chunk;
});
response.on('end',function(){
//write data here;
//do the callback
cb();
});
});
}
I solved the ROOT cause of the issue that I was seeing, though I do believe without the callback assistance from red above, I would have been just as lost.
Turns out the data was processing correctly, but the file write was scrambling. Turns out that there is a different method to call instead of writeFile or appendFile:
fs.appendFileSync();
Calling the Synchronous version processed the writes to the file IN THE ORDER they got appended to the file, instead of just going for it. This, in addition to the callback help above, solved the issue.
Thanks to everyone for the assistance!

Categories

Resources