2017-08-17 81 views
0

我需要從類似於此的JSON對象中獲取所有屬性中的最大值和最小值,這些值以結尾。我期望得到18作爲最大值,3作爲最小值。如何使用通配符從JSON對象中的任何屬性獲取最大值和最小值

我能夠獲得個人財產(Getting max value(s) in JSON array)的最大值,但不能以的值爲

[ 
{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} 
] 
+0

這將是你的數據的預期的結果組? – felixmosh

回答

1

你可以通過整個數組循環,並得到鑰匙爲每個對象,使用indexOf()檢查「計數」是關鍵,然後檢查它是否是最大。是這樣的:

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

+0

謝謝!非常簡短 – stanggt3

+0

@ stanggt3歡迎您考慮接受答案,如果它有幫助。乾杯! – Dij

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

希望這有助於!

1

寫的擴展版本只是爲了看看各部分的作品,從這裏可以修改根據自己的喜好,希望這有助於:

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) { 
 
\t var maxVal; 
 
\t for(var i = 0; i < arr.length; i++) { 
 
\t \t var obj = arr[i]; 
 
\t \t var matches = getFieldsThatEndWith(obj, ending); 
 
\t \t for (var j = 0; j < matches.length; j++) { 
 
\t \t \t if (!maxVal || parseInt(obj[matches[j]]) > maxVal) { 
 
\t \t \t \t maxVal = obj[matches[j]]; 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
\t return maxVal; 
 
} 
 

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

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

 
console.log(maxValue);

相關問題