- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 07:24 AM
i need to know that why this script is not being print the log detail ( gs.info("print value1:")). is it any issue.
How to remove the blank value from array ? please help.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 07:36 AM
Hi,
The problem is because the values in the array are not Strings. They are Integers.
Therefore you get an undefined when you use the check if(userArray[u].length > 0){
If you would adjust to convert it to string, then it will work just fine.
if(userArray[u].toString().length > 0){
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 07:55 AM
Try the below code to eliminate empty and duplicate values
var arruser=[1,2,3,4,5, ,];
var newArray = [];
for(var i in arruser) {
if(arruser[i]){ //This will eliminate empty values
newArray.push(arruser[i]); //Push values into new array
}
}
gs.info(newArray);
var arrayUtil = new global.ArrayUtil();
userArray = arrayUtil.unique(newArray);
gs.info('userArray.length after unique = ' + newArray.length); //Log newArray length
for (var u = 0; u < newArray.length; u++) {
if(newArray[u].toString().length > 0){
gs.info("print value1:");
}
}
Output

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 07:36 AM
Hi,
The problem is because the values in the array are not Strings. They are Integers.
Therefore you get an undefined when you use the check if(userArray[u].length > 0){
If you would adjust to convert it to string, then it will work just fine.
if(userArray[u].toString().length > 0){
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 07:44 AM
Hello,
Please use the below:-
var arruser=[1,2,3,4,5, ,];
var arrayUtil = new global.ArrayUtil();
userArray = arrayUtil.unique(arruser);
gs.info('userArray.length after unique = ' + userArray.length);
var filtered = userArray.filter(function (el) {
return el != null;
});
gs.info(filtered);
Please mark my answer as correct based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 10:04 AM
Thanks: below Code is working.
var arruser=[];
var gr= new GlideAggregate('sn_vulc_result');
gr.addAggregate('COUNT');
gr.groupBy('cmdb_ci');
gr.query();
gs.info("get Count:"+gr.getRowCount());
while(gr.next()){
var grCi= new GlideRecord('cmdb_ci');
grCi.addQuery('sys_id',gr.cmdb_ci);
grCi.query();
while (grCi.next()){
//gs.info("CI Manager:"+grCi.u_cimanager.manager.getDisplayValue());
arruser.push(grCi.u_cimanager.manager);
}
}
// new code will start here
var arrayUtil = new global.ArrayUtil();
userArray = arrayUtil.unique(arruser);
gs.warn('userArray.length after unique = ' + userArray.length);
var u;
for (u = 0; u < userArray.length; u++) {
if(userArray[u].toString().length > 0){
gs.info("print value1:"+userArray[u]);
var grGroupUser = new GlideRecord('sys_user_grmember');
var qGU1 = grGroupUser.addQuery('group', 'b85d44954a3623120004689b2d5dd60a');
qGU1.addCondition('user', userArray[u]);
grGroupUser.query();
if(!grGroupUser.hasNext()) {
grGroupUser.user = userArray[u];
grGroupUser.group = 'b85d44954a3623120004689b2d5dd60a';
grGroupUser.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 07:55 AM
Try the below code to eliminate empty and duplicate values
var arruser=[1,2,3,4,5, ,];
var newArray = [];
for(var i in arruser) {
if(arruser[i]){ //This will eliminate empty values
newArray.push(arruser[i]); //Push values into new array
}
}
gs.info(newArray);
var arrayUtil = new global.ArrayUtil();
userArray = arrayUtil.unique(newArray);
gs.info('userArray.length after unique = ' + newArray.length); //Log newArray length
for (var u = 0; u < newArray.length; u++) {
if(newArray[u].toString().length > 0){
gs.info("print value1:");
}
}
Output