简体   繁体   中英

How to get the max and min Value from any property in a JSON object with wildcard

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}
]

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

 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM