- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2021 07:22 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2021 04:38 AM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2021 12:10 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2021 12:22 PM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2021 12:01 AM
Hi Brad,
Thank you. The code works fine but doesn't push the last sorted record of an array.
logs:
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}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2021 04:38 AM
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));