Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript loop thru large record set in NodeJS

Status
Not open for further replies.

scripter50

IS-IT--Management
Dec 9, 2002
35
0
0
US
I have an Oracle query in a NodeJS function and having trouble with the loop thru the record set. Nodejs, hence javascript stuff.
Example: results from query of table with 20 columns, on of which is "ticket".

I loop thru the record set and stuff the values into a mainArray
for(var i in result) {
mainArray.push(result.rows);
}

So now I have a array to work with. I need to loop thru the array, and for every 30 records, grab the "ticket" value
and stuff it into a separate ticketArray. So I would end up with:
ticketArray[1] = '1', '2', '3', ....'30'
ticketArray[2] = '31', '32', '33',....'60'
ticketArray[3] = '62', '63', '64',.....'90' etc....

I can get the first 30 records, but lost on how to get the other values and put them into separate arrays.
Thanks in advance for any help!
 
Have two counters
One increments every iteration. When you get to 30 increment the second counter and reset the first to zero
Use the second counter to assign data points to your multidimensional array.
Instead of resetting the first counter to zero you could also test the modulus.
 
Still confused, this is what I have, but getting an error saying "arrTicketList[arrTicketList.length] is not defined"
This is the result set of an Oracle query, 20 columns, I need column 4 for example

var recordCount = arrCBUS.length; (say 100 for example)
var arrTicketList = [];
var maxRecordsPerIteration = 10;
var counter = 0;
var ticketList = '';

for(var i=0; i<recordCount; i++) {
if(counter < maxRecordsPerIteration) {
ticketList += arrCBUS[3] + ',';
} else {
console.log(ticketList); // this returns correctly, I get a string of 10 items
arrTicketList[arrTicketList.length].push(ticketList); // this is where it errors out
//reset the counter
counter = 0;
//clear the current ticketList variable so it starts over
ticketList = '';
//lastly start the list over with the current ticket number
ticketList += arrCBUS[3] + ',';
}
counter++;
}
 
arrTicketList doesn't have a 'length' to return the value of, and "pushing" a value to an array will add it at the end position regardless of 'length'.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks ChrisHirst,
I think I understand what you are saying but I'm still in a pickle. Somehow I need to create an array of arrays. As I iterate thru the arrCBUS, I want create an an array of the selected value (arrCBUS[3] is a ticket number I'm after). The reason for the record limits is that I'm getting back 2000 records and I'll break it down to 100 records at a time and run a nodejs function against the array.
arrTicketList[0] = [tkt1,tkt2,tk3,tk4.....etc]
arrTicketList[1] = [tkt1,tkt2,tk3,tk4.....etc]
arrTicketList[2] = [tkt1,tkt2,tk3,tk4.....etc]

So if I try just using the "i" value from the loop, I get the same results that arrTicketList is not defined. I initialize arrTicketList = []. Doesn't that define it?
 
Code:
var temp = [];
var holder = [];
for (var i in arrCBus) {
	if ( i>0 && i % 20 == 0) { //20 records per set
		holder.push(temp);
		temp = [];
	}
	temp.push(arrCBus[i][3]);	//3 is the targetField
}
if(temp.length > 0) holder.push(temp);
console.log(holder);
 
It defines it certainly, but it does not give it dimensions, it is just an array in memory, with no elements, therefore has no length.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top