let arr = [{
p:'p1',
score:[1,21,2,3,8,21]
},
{
p:'p2',
score:[1,22,2,4,8,21]
},
{
p:'p3',
score:[1,25,2,3,9,21]
}]
function delMaxMin(arr){
arr.forEach(i => {
let max=Math.max(...i.score)
let min=Math.min(...i.score)
let maxIndex = i.score.findIndex(item=>item==max)
let minIndex = i.score.findIndex(item=>item==min)
i.score.splice(maxIndex,1)
i.score.splice(minIndex,1)
let total=i.score.reduce((pre,cur)=>pre+cur)/i.score.length
//toFixed(1) 四舍五入一位小数
console.log(`${i.p}的数组为${i.score},平均成绩为:${total.toFixed(1)}`);
});
}
delMaxMin(arr)
输出:
p1的数组为2,3,8,21,平均成绩为:8.5
p2的数组为2,4,8,21,平均成绩为:8.8
p3的数组为2,3,9,21,平均成绩为:8.8