javascript group array by field “Variable”, find average value for each group and…
- User
javascript group array by field “Variable”, find average value for each group and difference between max and min value for each group - const dat = [ { “Variable”: “Trimmed Can Height”, “Value”: “1” }, { “Variable”: “Trimmed Can Height”, “Value”: “165” }, { “Variable”: “Wtf”, “Value”: “10” }, { “Variable”: “zxc”, “Value”: “10” }, { “Variable”: “zxc”, “Value”: “10” }, { “Variable”: “zxc”, “Value”: “10” } ]
- Assistant
const groupByVariable = dat.reduce((acc, obj) => { const key = obj.Variable; if (!acc[key]) { acc[key] = []; } acc[key].push(Number(obj.Value)); return acc; }, {});
const result = Object.keys(groupByVariable).map(key => { const values = groupByVariable[key]; const average = values.reduce((a, b) => a + b, 0) / values.length; const max = Math.max(...values); const min = Math.min(...values); const diff = max - min; return { Variable: key, Average: average, Difference: diff }; });
console.log(result);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.