How to print elements of an object in a script?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2017 02:49 PM
I want to print the element of my object? How do I do that?
In my case I want to print the element of my object 'recipients'?
var listCI = [];
var recipients= { }
function addRecipients( user, listCI){
recipients[user].push(listCI);
}
if(current.variables.impacted_ci_first.hasValue())
{
listCI.push(current.variables.impacted_ci_first);
}
if (current.variables.impacted_ci_second.hasValue()){
listCI.push(current.variables.impacted_ci_second);
}
if (current.variables.impacted_ci_third.hasValue() ){
listCI.push(current.variables.impacted_ci_third);
}
if (current.variables.impacted_ci_fourth.hasValue()){
listCI.push(current.variables.impacted_ci_fourth);
}
var arrayLength = listCI.length;
for ( var i= 0; i < arrayLength ; i++){
gs.print ('CI : ' + listCI[i].name);
}
var arrayLength = listCI.length;
for ( var i =0; i < arrayLength ; i++){
var ciName = listCI[i].name.toString();
addRecipients( listCI[i].owned_by.toString(), ciName);
gs.print ('Solution Managers :' + listCI[i].owned_by.name);
var group = listCI[i].support_group.toString();
gs.print ('Group :' + listCI[i].support_group.name);
var members;
var gd = new GlideRecord('sys_user_grmember');
gd.addQuery('group', listCI[i].support_group.toString());
gd.query();
while(gd.next()){
addRecipients(gs.user.toString(), ciName);
gs.print('Support Group Members: ' + ',' + gd.user.name);
}
}
//How to print elements of objects?
var array = recipients.length;
for ( var i=0; i < array; i++){
gs.print('Recepients :' + recipients[i]);
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2017 06:03 AM
Hi Claudia,
Once you implement the fix i mentioned in : Re: what's wrong with this for loop?
The 'recipients' array contains another array. But the 'recipients' array is associative, not a normal array indexed by integers. its indexed by strings. So nyou cant use :
for ( var i=0; i < array; i++){
gs.print('Recepients :' + recipients[i]);
}
IF you know what you want to access you can do :
var len = recipients['index'].length;
for ( var i=0; i < len; i++){
gs.print('Recepients :' + recipients['index'][i]);
}
IF you want to print ALL of the arrays in the 'recipients' variable you can do this :
for (var user in recipients)
{
var len = recipients['user'].length;
for ( var i=0; i < len; i++){
gs.print('Recepients :' + recipients['user '][i]);
}
}
Cheers
Mohamad