We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to return count of objects from an array

Hari1
Mega Sage

Hi,

I have the below array of objects.

var arr = [
    {group: "AA", complexity: "Simple"},
    {group: "AA", complexity: "Complex"},
    {group: "AA", complexity: "Simple"},
    {group: "BB", complexity: "Simple"},
    {group: "AA", complexity: "Medium"}
];

I am trying to get the output as below:

[
    {group: "AA", complexity: "Simple",count: 2},
    {group: "AA", complexity: "Complex",count: 1},
    {group: "BB", complexity: "Simple",count: 1},
    {group: "AA", complexity: "Medium",count: 1}
]

Can anyone please help me achieve this?

1 ACCEPTED SOLUTION

When run as a Fix Script, I'm getting the full results in the array, and it's not showing an error.  Here's an alternate way that should avoid the error.

var arr = [
    {group: "AA", complexity: "Simple"},
    {group: "AA", complexity: "Complex"},
    {group: "AA", complexity: "Simple"},
    {group: "BB", complexity: "Simple"},
    {group: "AA", complexity: "Medium"}
];
var outputArr = [];
var outputObj = {};
var group = '';
var comp = '';
var count = 0;

//first re-order array on group and complexity
arr.sort(function (a, b) {
    return a.group.localeCompare(b.group) || a.complexity.localeCompare(b.complexity);
});

//loop through the sorted array to build the output array, comparing group and complexity previous values to establish correct count
for(var i=0; i<=arr.length; i++){
	if(i == arr.length){
		outputObj ={
					'group' : arr[i-1].group.toString(),
					'complexity' : arr[i-1].complexity.toString(),
					'count' : count
				};
				outputArr.push(outputObj);
	}
	else if(group == arr[i].group.toString()){
		if(comp == arr[i].complexity.toString()){
			count++;
		}
		else{
			outputObj ={
				'group' : arr[i-1].group.toString(),
				'complexity' : arr[i-1].complexity.toString(),
				'count' : count
			};
			outputArr.push(outputObj);
			comp = arr[i].complexity.toString();
			count = 1;
		}
	}
	else if(count == 0){
		group = arr[i].group.toString();
		comp = arr[i].comp.toString();
		count = 1;
	}
	else{
		outputObj ={
			'group' : arr[i-1].group.toString(),
			'complexity' : arr[i-1].complexity.toString(),
			'count' : count
		};
		outputArr.push(outputObj);
		group = arr[i].group.toString();
		comp = arr[i].complexity.toString();
		count = 1;
	}
}
gs.print(JSON.stringify(outputArr));

View solution in original post

8 REPLIES 8

Works like a charm. Thanks Brad.

You are welcome.

Hi Brad,

Is there a way to eliminate the duplicates based on the attribute "group". Below is the output that we are getting now.

[
    {group: "AA", complexity: "Simple",count: 2},
    {group: "AA", complexity: "Complex",count: 1},
    {group: "BB", complexity: "Simple",count: 1},
    {group: "AA", complexity: "Medium",count: 1}
]

Can we get something like the below.

[
    {group: "AA", simple: {complexity: "Simple",count: 2}, complex: {complexity: "Complex",count: 1} medium: {complexity: "Medium",count: 1}}, 
    {group: "BB", simple:{complexity: "Simple",count: 1}}
];

 

The requirement is to get the group complexities count like the below.

find_real_file.png

This script will get you close.  I don't know how or if you can conditionally show object elements, so you'll see each complexity for each group.

var arr = [
    {group: "AA", complexity: "Simple"},
    {group: "AA", complexity: "Complex"},
    {group: "AA", complexity: "Simple"},
    {group: "BB", complexity: "Simple"},
    {group: "AA", complexity: "Medium"}
];
var outputArr = [];
var outputObj = {};
var group = '';
var comp = '';
var compSimp = 0;
var compComp = 0;
var compMed = 0;

//first re-order array on group and complexity
arr.sort(function (a, b) {
    return a.group.localeCompare(b.group) || a.complexity.localeCompare(b.complexity);
});

//loop through the sorted array to build the output array, comparing group and complexity previous values to establish correct counts
for(var i=0; i<=arr.length; i++){
	if(i == arr.length){
		//last input array element, build the output array 
		outputObj ={
					'group' : arr[i-1].group.toString(),
					'simple' : {
						'complexity' : 'Simple',
						'count' : compSimp	
					},
					'complex' : {
						'complexity' : 'Complex',
						'count' : compComp	
					},
					'medium' : {
						'complexity' : 'Medium',
						'count' : compMed	
					}
				};
				outputArr.push(outputObj);
	}
	else if(group == arr[i].group.toString()){
		if(comp == arr[i].complexity.toString()){
			//same group and comp, so increase comp count
			if(comp == 'Simple'){
				compSimp++;
			}
			else if(comp == 'Medium'){
				compMed++;
			}
			else if(comp == 'Complex'){
				compComp++;
			}
		}
		else{
			//same group, different comp
			comp = arr[i].complexity.toString();
			if(comp == 'Simple'){
			compSimp = 1;
			}
			else if(comp == 'Medium'){
				compMed = 1;
			}
			else if(comp == 'Complex'){
				compComp = 1;
			}
		}
	}
	else if(i == 0){
		//first input array element
		group = arr[i].group.toString();
		comp = arr[i].complexity.toString();
		if(comp == 'Simple'){
			compSimp = 1;
		}
		else if(comp == 'Medium'){
			compMed = 1;
		}
		else if(comp == 'Complex'){
			compComp = 1;
		}
	}
	else{
		//this is the first element in this group, so build the array of the previous group now
		outputObj ={
			'group' : arr[i-1].group.toString(),
			'simple' : {
				'complexity' : 'Simple',
				'count' : compSimp	
			},
			'complex' : {
				'complexity' : 'Complex',
				'count' : compComp	
			},
			'medium' : {
				'complexity' : 'Medium',
				'count' : compMed	
			}
		};
		outputArr.push(outputObj);
		//set vars to new group and complexity
		group = arr[i].group.toString();
		comp = arr[i].complexity.toString();
		if(comp == 'Simple'){
			compSimp = 1;
			compMed = 0;
			compComp = 0;
		}
		else if(comp == 'Medium'){
			compMed = 1;
			compSimp = 0;
			compComp = 0;
		}
		else if(comp == 'Complex'){
			compComp = 1;
			compSimp = 0;
			compMed = 0;
		}
	}
}
gs.print(JSON.stringify(outputArr));
{"group":"AA","simple":{"complexity":"Simple","count":2},"complex":{"complexity":"Complex","count":1},"medium":{"complexity":"Medium","count":1}},
{"group":"BB","simple":{"complexity":"Simple","count":1},"complex":{"complexity":"Complex","count":0},"medium":{"complexity":"Medium","count":0}}]