Tengo una matriz de objetos con diferentes valores, quiero encontrar el mínimo, el máximo y el promedio de las propiedades en esa matriz
por ejemplo, si tengo una matriz
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" } ]Entonces la salida será como se indica a continuación.
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 }]Entonces, para obtener el resultado, iteramos a la matriz y encontramos el valor mínimo, máximo y promedio de a, b, c y d y lo almacenamos en una nueva matriz
Aquí hay un enfoque de dos pasos. Primero agrupé la matriz en propiedad usando reduce . Luego realicé un map en el objeto agrupado para obtener el mínimo, el máximo y el promedio
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)Utilice la siguiente función: `
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 })`
He seguido un proceso de dos pasos:
reduce para esto) 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);