[英]How to find the minimum, maximum and average value of all property of array of objects
我有一個包含不同值的對象數組,我想找到該數組中屬性的最小值、最大值和平均值
例如,如果我有一個數組
const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", "d": "-0.14" }, { "a": "-0.37", "b": "0.01", "c": "-0.77", "d": "-0.09" }, { "a": "0.01", "b": "0.88", "c": "-0.53", "d": "-0.28" }, { "a": "0.53", "b": "-0.62", "c": "0.02", "d": "0.74" }, { "a": "0.79", "b": "-0.39", "c": "0.70", "d": "0.18" }, { "a": "0.74", "b": "-0.14", "c": "0.22", "d": "-0.58" } ]
所以輸出將如下所示
const out = [{ property: a, minValue: -0.37, maxValue: .79, avg: 0.2733333333333334 }, { property: b, minValue: -.62, maxValue: .88, avg: -0.0016666666666666496 }, { property: c, minValue: -.96, maxValue: .07, avg: -0.21999999999999997 }, { property: d, minValue: -.58, maxValue: .74, avg: -0.028333333333333332 }]
因此,為了獲得輸出,我們迭代數組並找到 a、b、c 和 d 的最小值、最大值和平均值,並將其存儲在一個新數組中
這是一個兩步的方法。 首先,我使用reduce
對屬性上的數組進行分組。 然后我在分組對象上執行了一個map
來獲得最小值、最大值和平均值
const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", "d": "-0.14" }, { "a": "-0.37", "b": "0.01", "c": "-0.77", "d": "-0.09" },{ "a": "0.01", "b": "0.88", "c": "-0.53", "d": "-0.28" }, { "a": "0.53", "b": "-0.62", "c": "0.02", "d": "0.74" }, { "a": "0.79", "b": "-0.39", "c": "0.70", "d": "0.18" }, { "a": "0.74", "b": "-0.14", "c": "0.22", "d": "-0.58" }] const grouped = array.reduce((acc,curr) => { Object.entries(curr).forEach(([property,v])=>{ acc[property] = acc[property] || [] acc[property].push(+v) //+ for convert to a number before pushing }) return acc },{}) const out = Object.entries(grouped).map(([property,v]) => { return { property, minValue: Math.min(...v), // min of an array. v is the array of numbers maxValue: Math.max(...v), //max of an array avg: v.reduce((a, b) => a + b) / v.length //average of an array } }) console.log(out)
您使用 Array.reduce 並從一個類似於結果但具有極值的對象開始,因此對於 min 有最大值。 或者,根據您的規范,它對於任何空輸入數組都是未定義的。
這是一種方法,首先我們制作一個看起來像這樣的地圖
{ a : ['-0.06', '-0.06', '-0.37', '0.01', '0.53', '0.79', '0.74']
b: ['0.25', '0.25', '0.01', '0.88', '-0.62', '-0.39', '-0.14'],
...
}
然后我們對地圖中的每個值運行一個循環以找到最大值、最小值和平均值。
const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", "d": "-0.14" }, { "a": "-0.37", "b": "0.01", "c": "-0.77", "d": "-0.09" },{ "a": "0.01", "b": "0.88", "c": "-0.53", "d": "-0.28" }, { "a": "0.53", "b": "-0.62", "c": "0.02", "d": "0.74" }, { "a": "0.79", "b": "-0.39", "c": "0.70", "d": "0.18" }, { "a": "0.74", "b": "-0.14", "c": "0.22", "d": "-0.58" }] const getStats = (data) => { const valueMap = {}; data.forEach((item) => { Object.entries(item).forEach(([key, value]) => { if (!valueMap[key]) { valueMap[key] = [value]; } valueMap[key].push(value); }); }); return Object.entries(valueMap).map(([key, value]) => ({ property: key, ...getMinMaxAndAvg(value) })); }; const getMinMaxAndAvg = (arr) => { let minValue = Number.MAX_VALUE; let maxValue = Number.MIN_VALUE; let total = 0; arr.forEach((val) => { if (val > maxValue) { maxValue = val; } if (val < minValue) { minValue = val; } total += +(val); }); return { minValue, maxValue, avg: total / arr.length }; }; console.log(getStats(array));
使用以下函數:`
const createNewObj = (previous, current) => ({
minValue: Math.min(previous, current,
maxValue: Math.max(previous, current,
total: previous + current,
length: previous.length + 1,
avg: (previous + current) / (previous.length + 1)
})
const default = {
minValue: 0,
maxValue: 0,
total: 0,
length: 0,
avg: 0
}
array.reduce((previous, current) => {
a: createNewObj(previous[a], current[a]),
b: createNewObj(previous[b], current[b]),
c: createNewObj(previous[c], current[c]),
d: createNewObj(previous[d], current[d])
}
, {
a: default,
b: default,
c: default,
d: default
})
`
我遵循了一個兩步過程-
reduce
) const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", "d": "-0.14" }, { "a": "-0.37", "b": "0.01", "c": "-0.77", "d": "-0.09" },{ "a": "0.01", "b": "0.88", "c": "-0.53", "d": "-0.28" }, { "a": "0.53", "b": "-0.62", "c": "0.02", "d": "0.74" }, { "a": "0.79", "b": "-0.39", "c": "0.70", "d": "0.18" }, { "a": "0.74", "b": "-0.14", "c": "0.22", "d": "-0.58" }] const out = []; const newObj = array.reduce((prevValue, currValue) => { Object.keys(currValue).forEach((el) => { if (prevValue[el]) { prevValue[el].push(Number(currValue[el])); } else { prevValue[el] = [Number(currValue[el])]; } }); return prevValue; }, {}); Object.keys(newObj).forEach((el) => { out.push({ property: el, minValue: Math.min(...newObj[el]), maxValue: Math.max(...newObj[el]), avg: newObj[el].reduce((a, b) => a + b, 0) / newObj[el].length }); }); console.log(out);
const array = [{
"a": "-0.06",
"b": "0.25",
"c": "-0.96",
"d": "-0.14"
},
{
"a": "-0.37",
"b": "0.01",
"c": "-0.77",
"d": "-0.09"
},
{
"a": "0.01",
"b": "0.88",
"c": "-0.53",
"d": "-0.28"
},
{
"a": "0.53",
"b": "-0.62",
"c": "0.02",
"d": "0.74"
},
{
"a": "0.79",
"b": "-0.39",
"c": "0.70",
"d": "0.18"
},
{
"a": "0.74",
"b": "-0.14",
"c": "0.22",
"d": "-0.58"
}
]
var out = [
]
Object.keys(array[0]).map(v=>{//this is so it runs for all 4 properties
let temp = {
property:v
}
//sort array to get min and max
let ary = [...array].sort((a,b)=>{
return a-b
})
/* console.log(ary) */
let avg = 0;
ary.map(i=>{
avg = avg+Number(i[v]);
})
temp.avg = avg/array.length
temp.minValue = ary.pop()[v]
temp.maxValue = ary.shift()[v]
out.push(temp)
})
console.log(out)
見菲德爾
以下是您可以使用的代碼片段
const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", "d": "-0.14" }, { "a": "-0.37", "b": "0.01", "c": "-0.77", "d": "-0.09" }, { "a": "0.01", "b": "0.88", "c": "-0.53", "d": "-0.28" }, { "a": "0.53", "b": "-0.62", "c": "0.02", "d": "0.74" }, { "a": "0.79", "b": "-0.39", "c": "0.70", "d": "0.18" }, { "a": "0.74", "b": "-0.14", "c": "0.22", "d": "-0.58" } ] const grouped = array.reduce((acc, curr) => { for(let key in curr) { if(!acc[key]) { acc[key] = [] }else { acc[key].push(curr[key]) } } return acc; }, {}) const output = [] for(let key in grouped) { const obj = {} const integers = grouped[key].map(el => parseFloat(el, 10)) obj["property"] = key obj["minValue"] = Math.min(...integers) obj["maxValue"] = Math.max(...integers) obj["avg"] = (integers.reduce((prev, curr) => prev + curr) ) / integers.length; output.push(obj) } console.log(output)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.