0

I need to get the max and min values from all properties that end in Count from a JSON object similar to this. I would expect to get 18 as the max and 3 as the min.

I am able to get the max for an individual property (Getting max value(s) in JSON array) but not for anything ending in Count.

[
{Period: 01/2017, Alpha: 5, AlphaCount: 10, Bravo: 3, BravoCount: 7, Charlie: 9, CharlieCount: 3}
{Period: 02/2017, Alpha: 2, AlphaCount: 4, Bravo: 3, BravoCount: 6, Charlie: 9, CharlieCount: 13}
{Period: 03/2017, Alpha: 6, AlphaCount: 8, Bravo: 3, BravoCount: 12, Charlie: 9, CharlieCount: 18}
]
BSMP
  • 4,596
  • 8
  • 33
  • 44
stanggt3
  • 63
  • 4

3 Answers3

1

you can loop through the entire array and get keys for each object, use indexOf() to check if 'Count' is in the key and then check if it is max. something like this:

var arr = [
{Period: 01/2017, Alpha: 5, AlphaCount: 10, Bravo: 3, BravoCount: 7, Charlie: 9, CharlieCount: 3},
{Period: 02/2017, Alpha: 2, AlphaCount: 4, Bravo: 3, BravoCount: 6, Charlie: 9, CharlieCount: 13},
{Period: 03/2017, Alpha: 6, AlphaCount: 8, Bravo: 3, BravoCount: 12, Charlie: 9, CharlieCount: 18}
];
var max = -1;
arr.forEach(function(ob){
   var keys = Object.keys(ob);
   keys.forEach(x => max = x.indexOf('Count')>=0 && ob[x] >max ? ob[x]:max);
})
console.log(max);
Dij
  • 9,761
  • 4
  • 18
  • 35
1

const sample = [
  {Period: '01/2017', Alpha: 5, AlphaCount: 10, Bravo: 3, BravoCount: 7, Charlie: 9, CharlieCount: 3},
  {Period: '02/2017', Alpha: 2, AlphaCount: 4, Bravo: 3, BravoCount: 6, Charlie: 9, CharlieCount: 13},
  {Period: '03/2017', Alpha: 6, AlphaCount: 8, Bravo: 3, BravoCount: 12, Charlie: 9, CharlieCount: 18}
]

const arr = []
function extractCount (item) {
  return Object.keys(item).forEach(i =>  {
    /Count$/.test(i) && arr.push(item[i]);
  })
}

sample.map(extractCount)

console.log(Math.max(...arr));
console.log(Math.min(...arr));

Hope this helps!

Rowland
  • 1,728
  • 1
  • 12
  • 19
1

Wrote an extended version just to see how each part works, from here you can modify to your liking, hope this helps:

var arr = [
{Period: 01/2017, Alpha: 5, AlphaCount: 10, Bravo: 3, BravoCount: 7, Charlie: 9, CharlieCount: 3},
{Period: 02/2017, Alpha: 2, AlphaCount: 4, Bravo: 3, BravoCount: 6, Charlie: 9, CharlieCount: 13},
{Period: 03/2017, Alpha: 6, AlphaCount: 8, Bravo: 3, BravoCount: 12, Charlie: 9, CharlieCount: 18}
];

function getMaxThatEndsWith(arr, ending) {
 var maxVal;
 for(var i = 0; i < arr.length; i++) {
  var obj = arr[i];
  var matches = getFieldsThatEndWith(obj, ending);
  for (var j = 0; j < matches.length; j++) {
   if (!maxVal || parseInt(obj[matches[j]]) > maxVal) {
    maxVal = obj[matches[j]];
   }
  }
 }
 return maxVal;
}

function getFieldsThatEndWith(obj, like) {
 var matches = [];
 var propertyNames = Object.getOwnPropertyNames(obj);
 for(var i = 0; i < propertyNames.length; i++) {
  var propName = propertyNames[i];
  if (propName.endsWith(like)) {
   matches.push(propName);
  }
 }
 return matches;
}

var maxValue = getMaxThatEndsWith(arr, 'Count');

console.log(maxValue);
Dan D
  • 2,493
  • 15
  • 23