How to create an array of arrays in javascript from gliderecord results

anfield
Tera Guru

Running a fix script which returns about 300 records, each record will return 4 fields. What I want to do is put the entire contents in one array, and then for each record put that into an individual array. I know what I have below doesnt work but I wanted some guidance on how to accomplish this? 

I am creating my first array before the glide record while, based on the number of records returned, but I am trying to figure out how to populate it. The 2 nested for loops below should be fine to iterate over the data, but I need to build the nested array first.  I know how to add the data into an ordinary array with using push, but its the nested part thats giving me trouble.

The arrays should look like this:

old_computers = [

[field1 ,field2 ,field3 , field4],

[field1,field2,field3,field4]

];

I will get the field data after the glide record query like this:

var compname = grw.getValue('name');

var serial = grw.getValue('serial_number');

 

Fix Script:

function x_days_or_older(){

var grw = new GlideRecord('cmdb_ci_computer');
grw.addQuery('sys_class_name','=', 'cmdb_ci_computer');

// Not equal to decommissioned
grw.addQuery('operational_status', '=', 5);
grw.addQuery('discovery_source','=', 'MS SMS');
grw.addQuery('u_sccm_last_scan','!=','');

grw.addEncodedQuery('sys_class_name=cmdb_ci_computer^u_sccm_last_scanRELATIVELT@dayofweek@ago@500');

grw.query();

gs.log("sccm last scan number: " + grw.getRowCount());


var old_computers_all = new Array (grw.getRowCount());

while (grw.next()) {

for (var i=0; i < old_computers_all.length; i++){
         for (var j=0; j < old_computers_all[i].length; j++);{
                gs.log('SCCM olderscans: inner loop: ' + old_computers_all[i][j]);

}


1 ACCEPTED SOLUTION

In your original loop change it to:

 while (grw.next()) {
        var tempArray = []
        tempArray.push(grw.getValue('name') + '');
        tempArray.push(grw.getValue('serial_number') + ''); 
        old_computers_all.push(tempArray); 
    }

Are you sure your field names are correct?

View solution in original post

13 REPLIES 13

Did you update the script to the above?

Hi elijah. Yes I made the changes you suggested but the output is the exact same. The log near the end for inner loop just keeps showing undefined for each record. 452 undefined now

Heres the script now

 

function x_days_or_older(){

var grw = new GlideRecord('cmdb_ci_computer');
grw.addQuery('sys_class_name','=', 'cmdb_ci_computer');

// Not equal to decommissioned
grw.addQuery('operational_status', '!=', 50);
grw.addQuery('discovery_source','=', 'MS SMS');
grw.addQuery('chassis_type','!=', 'Notebook');
grw.addQuery('chassis_type','!=', 'Laptop');


grw.addEncodedQuery('sys_class_name=cmdb_ci_computer^u_sccm_last_scanRELATIVELT@dayofweek@ago@500');

grw.query();

gs.log("sccm last scan number: " + grw.getRowCount());
gs.log('SCCM: before while');


var old_computers_all = [];

gs.log('SCCM olderscans: array create size: ' + old_computers_all.length);

while (grw.next()) {

var one_computer = [];

one_computer.push(grw.getValue('correlation_id') +'');
one_computer.push(grw.getValue('name') + '');
one_computer.push(grw.getValue('serial_number')+ '');
one_computer.push(grw.getValue('u_sccm_last_scan')+ '');

old_computers_all.push(one_computer);
gs.log('SCCM olderscans: Counter: ' + old_computers_all.length);

}gs.log('SCCM olderscans: 1st func: ' + old_computers_all.length);
return old_computers_all;


}

function unpack() {

gs.log('SCCM olderscans inside unpack: ');

var old_computers_all = x_days_or_older();

gs.log('SCCM Olderscans: Unpack: length: ' + old_computers_all.length);

for (var i=0; i < old_computers_all.length; i++){

for (var j=0; j < old_computers_all[i].length; j++);{
gs.log('SCCM olderscans: inner loop: ' + old_computers_all[i][j]);

}

}

}unpack();

Ok. I removed the semi colon here from the inner loop and now I'm seeing the values printed in the inner loop log

 

for (var j=0; j < old_computers_all[i].length; j++);

Hi Elijah Aromola,

Great, just for sharing more contents about array...

 

Obs: anfield, Please mark Elijah like the correct answer .

 

//maintain this like out of while
 var tempArray = []

while (grw.next()) {
       
        tempArray.push(grw.getValue('name') + '');
        tempArray.push(grw.getValue('serial_number') + ''); 
        old_computers_all.push(tempArray); 
    }

Regards