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

harun_isakovic
Mega Guru

Your example adds to confusion but "arr.length" will return you the number of objects inside of it.

find_real_file.png

Brad Bowman
Kilo Patron
Kilo Patron

This code will sort the array then loop through it to build an output array containing the count of each group/complexity per your requirements.

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

Hi Brad,

Thank you. The code works fine but doesn't push the last sorted record of an array.

logs:

find_real_file.png

arr[5] has no record as the value of i starts from 0.

I need to get 

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

but the last record is not being pushed to the array i.e {group: "BB", complexity: "Simple","count":1}

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