我怎样才能找到对象数组中的最高和最低/ Javascript
How can I find highest and lowest in an array of objects/ Javascript
我是js新手,你能帮我找到对象数组中的最高温度和最低温度吗
let weather = [
{ month: 'March', temperature: [2,5,4,3,7,12]},
{ month: 'April', temperature: [14,15,16,19, 20]},
{ month: 'May', temperature: [22,24,26,28,27]}
]
我改变了对象并在对象本身中添加了 lowest
、'highest' 和 'average'。
为了获得 month
和 temperature
,我使用了 object destructuring。
然后使用 sort
函数对数组进行排序。
现在 tempArray 已排序,索引 0
处的元素是 lowest
,temporary.length - 1
处的元素是最高元素。
为了获取数组的平均值,我使用了数组 reduce 函数。
let weather = [
{ month: "March", temperature: [2, 5, 4, 3, 7, 12] },
{ month: "April", temperature: [14, 15, 16, 19, 20] },
{ month: "May", temperature: [22, 24, 26, 28, 27] },
];
let getAverage = (array) => array.reduce((a, b) => a + b) / array.length;
weather.forEach((monthData) => {
const { month, temperature } = monthData;
// temporary sorted array
const tempArray = [...temperature].sort((a, b) => a - b);
// Lowest
monthData.lowest = tempArray[0];
// Heighest
monthData.highest = tempArray[tempArray.length - 1];
// Average
monthData.average = getAverage(tempArray);
});
console.log(weather);
const averageArray = weather
.map((month) => month.average)
.sort((a, b) => a - b);
const lowestInAverage = averageArray[0];
const highestInAverage = averageArray[averageArray.length - 1];
console.log(lowestInAverage, highestInAverage);
使用地图和减少。内部地图回调使用 Math.max & Math.min 查找最高和最低温度,并使用 sort 按平均温度的升序对数组进行排序
let weather = [{
month: 'March',
temperature: [2, 5, 4, 3, 7, 12]
},
{
month: 'April',
temperature: [14, 15, 16, 19, 20]
},
{
month: 'May',
temperature: [22, 24, 26, 28, 27]
}
]
const val = weather.map(item => {
return {
month: item.month,
temperature: item.temperature,
lowest: Math.min(...item.temperature),
highest: Math.max(...item.temperature),
avgTemp: item.temperature.reduce((acc, curr) => {
return (acc + curr)
}, 0) / item.temperature.length
}
}).sort((a, b) => a.avgTemp - b.avgTemp)
console.log(val)
我是js新手,你能帮我找到对象数组中的最高温度和最低温度吗
let weather = [
{ month: 'March', temperature: [2,5,4,3,7,12]},
{ month: 'April', temperature: [14,15,16,19, 20]},
{ month: 'May', temperature: [22,24,26,28,27]}
]
我改变了对象并在对象本身中添加了 lowest
、'highest' 和 'average'。
为了获得 month
和 temperature
,我使用了 object destructuring。
然后使用 sort
函数对数组进行排序。
现在 tempArray 已排序,索引 0
处的元素是 lowest
,temporary.length - 1
处的元素是最高元素。
为了获取数组的平均值,我使用了数组 reduce 函数。
let weather = [
{ month: "March", temperature: [2, 5, 4, 3, 7, 12] },
{ month: "April", temperature: [14, 15, 16, 19, 20] },
{ month: "May", temperature: [22, 24, 26, 28, 27] },
];
let getAverage = (array) => array.reduce((a, b) => a + b) / array.length;
weather.forEach((monthData) => {
const { month, temperature } = monthData;
// temporary sorted array
const tempArray = [...temperature].sort((a, b) => a - b);
// Lowest
monthData.lowest = tempArray[0];
// Heighest
monthData.highest = tempArray[tempArray.length - 1];
// Average
monthData.average = getAverage(tempArray);
});
console.log(weather);
const averageArray = weather
.map((month) => month.average)
.sort((a, b) => a - b);
const lowestInAverage = averageArray[0];
const highestInAverage = averageArray[averageArray.length - 1];
console.log(lowestInAverage, highestInAverage);
使用地图和减少。内部地图回调使用 Math.max & Math.min 查找最高和最低温度,并使用 sort 按平均温度的升序对数组进行排序
let weather = [{
month: 'March',
temperature: [2, 5, 4, 3, 7, 12]
},
{
month: 'April',
temperature: [14, 15, 16, 19, 20]
},
{
month: 'May',
temperature: [22, 24, 26, 28, 27]
}
]
const val = weather.map(item => {
return {
month: item.month,
temperature: item.temperature,
lowest: Math.min(...item.temperature),
highest: Math.max(...item.temperature),
avgTemp: item.temperature.reduce((acc, curr) => {
return (acc + curr)
}, 0) / item.temperature.length
}
}).sort((a, b) => a.avgTemp - b.avgTemp)
console.log(val)