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

Elijah Aromola
Mega Sage

If I'm following what you're attempting to do, your loop would look like:

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

Thanks that does help. I was trying to pass the results of that first function to a second function though and just iterate through the results and I'm just seeing undefined (for each record). Do my two for loops below look correct? I've confirmed that I am seeing the correct number of records when I log the length before the for loop but I'm not sure why the loop below isnt working.

 

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

This line: var old_computers_all = x_days_or_older(); Does x_days_or_older() return array? If yes, then assign this result to your old_computers_all shall be like this var old_computers_all = x_days_or_older().slice(0);

use OOTB function to get confirm whether it returns array or not as follow.

Array.isArray(x_days_or_older()); if it returns true then we are good.