INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Javascript loop thru large record set in NodeJS

Javascript loop thru large record set in NodeJS

(OP)
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[i]);
}

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!

RE: Javascript loop thru large record set in NodeJS

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.

RE: Javascript loop thru large record set in NodeJS

(OP)
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[i][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[i][3] + ',';
}
counter++;
}

RE: Javascript loop thru large record set in NodeJS

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

RE: Javascript loop thru large record set in NodeJS

(OP)
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[i][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?

RE: Javascript loop thru large record set in NodeJS

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); 

RE: Javascript loop thru large record set in NodeJS

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close