SUBHAM_SHAW_SN
Tera Guru
Tera Guru

Purpose : To simplify Array of Object syntaxing /formatting

 

Writing/maintaining array of objects has always been a challenge for developers. This is beacuse of the fact that it has a lot of formatting involved and it's also difficult to maintain when the size increases. A typical array of object looks something like below :

 

 

var incidents= [
  {
    "number": INC00XXX1
    "short_description": "Access Issue",
    "category": "Access",
    "sub-category": "Platform Issue",
  },
  {
    "number": INC00XXX2
    "short_description": "Issue with accessing emails",
    "category": "Office 365",
    "sub-category":  "Outlook not synching",
  },
  {
    ...
  },
  ...
]

 

 

Challenge :

 

The above code represents an array of objects which contains details related to multiple INCs. The major challenge here is the maintainability in terms of both formatting as well as coping up with the increase in array size.

 

Solution:

 

The  below coding startegy could be used in order to make it look easy to write and maintain :

 

var incidents=[];

var firstIncident={};
firstIncident.number=INC00XXX1;
firstIncident.short_description="Access Issue";
firstIncident.category="Access";
firstIncident.sub-category="Platform Issue";

incidents.push(firstIncident);

var secondIncident={};
secondIncident.number=INC00XXX2;
secondIncident.short_description="Issue with accessing emails";
secondIncident.category="Office 365";
secondIncident.sub-category="Outlook not synching";

incidents.push(secondIncident);

... So on !

 

The code above does the same thing as earlier but more into an easily formattable and maintainable way.

 

 

 

Thanks & Regards ,

Subham Kumar Shaw

Senior ServiceNow Consultant

ServiceNow Community Rising Star '2022/2023

 

 

 

 

 

Version history
Last update:
‎06-15-2023 03:55 AM
Updated by:
Contributors