Javascript - 尝试不可变地向地图中的数组添加数据但收到空数组
Javascript - trying to add data to array in map immutably but receiving empty array
我有一个函数,我试图通过一组对象进行映射。对于数组中的每个项目,我想计算某些平均值,然后将该数据弹出到不同的数组中。在函数的末尾,我想要 return 现在包含映射期间接收到的数据的数组。
相反,我return编辑了一个空数组。
我试图在不改变数组的情况下这样做,所以避免使用 Array.push() (我会注意到,如果我在我的函数中使用 Array.push 来更新我的数组,那么函数就可以工作).
这里是有问题的函数:
function getForecastAverages(forecasts) {
const dailyForecasts = Object.values(forecasts);
const averages = [];
dailyForecasts.map((hourlyForecasts, idx) => {
const high = getTempAvg(hourlyForecasts, 'high');
const low = getTempAvg(hourlyForecasts, 'low');
const desc = getDescriptionAvg(hourlyForecasts);
return [...averages, { date: Object.keys(forecasts)[idx], high, low, desc }];
});
return averages;
}
其他功能
function getTempAvg(forecasts, key) {
const avg = forecasts.reduce((acc, curr) => acc + curr[key], 0) / forecasts.length;
return avg.toFixed(2);
}
function getDescriptionAvg(forecasts) {
const length = forecasts.length;
const descriptions = forecasts.map(forecast => forecast.description);
return getMostFrequent(descriptions, length, 0);
}
function getMostFrequent(array, length, currIdx, maxCount = 0, mostFrequent = null, counts = {}) {
if (currIdx + 1 > length) return mostFrequent;
// first we need the word that we are counting the frequency of in the array
const word = array[currIdx];
// check if the current word exists in our counts object. If it doesn't, add the word and its count to the object. If it is present we increase its current count by 1
if (counts[word] === undefined) {
counts[word] = 1;
} else {
counts[word] = counts[word] + 1;
}
// check if the current word has a count that is larger than the highest count in our object. If it does we want to update our max count and most frequent word
if (counts[word] > maxCount) {
maxCount = counts[word];
mostFrequent = word;
}
const newIdx = currIdx + 1;
return getMostFrequent(array, length, newIdx, maxCount, mostFrequent, counts);
}
虚拟数据
forecasts = {
'2019-07-25': [
{
date: '2019-07-25 18:00:00',
description: 'broken clouds',
grnd_level: 1006.19,
high: 24.64,
humidity: 49,
low: 23.98,
pressure: 1017.03,
sea_level: 1017.03,
temp: 24.64,
},
{
date: '2019-07-25 21:00:00',
description: 'overcast clouds',
grnd_level: 1006.34,
high: 22.91,
humidity: 55,
low: 22.41,
pressure: 1017.16,
sea_level: 1017.16,
temp: 22.91,
},
],
'2019-07-26': [
{
date: '2019-07-26 18:00:00',
description: 'broken clouds',
grnd_level: 1006.19,
high: 24.64,
humidity: 49,
low: 23.98,
pressure: 1017.03,
sea_level: 1017.03,
temp: 24.64,
},
{
date: '2019-07-26 21:00:00',
description: 'overcast clouds',
grnd_level: 1006.34,
high: 22.91,
humidity: 55,
low: 22.41,
pressure: 1017.16,
sea_level: 1017.16,
temp: 22.91,
},
],
}
我也试过了
return averages.concat({ date: Object.keys(forecasts)[idx], high, low, desc })
收到错误消息 "averages" 是只读的,无法更改
const oldAvgs = averages
averages = [...oldAvgs, { date: Object.keys(forecasts)[idx], high, low, desc }]
return averages
有效,但正如我所说,它正在改变数组
return averages.push({ date: Object.keys(forecasts)[idx], high, low, desc })
您需要return映射对象
function getForecastAverages(forecasts) {
const dailyForecasts = Object.values(forecasts);
const dailyForecastKeys = Object.keys(forecasts);
const averages = dailyForecasts.map((hourlyForecasts, idx) => {
const high = getTempAvg(hourlyForecasts, 'high');
const low = getTempAvg(hourlyForecasts, 'low');
const desc = getDescriptionAvg(hourlyForecasts);
return { date: dailyForecastKeys[idx], high, low, desc };
});
return averages;
}
Javascript array map method returns 一个新的数组,你需要把你的代码稍微改成这样:
function getForecastAverages(forecasts) {
const dailyForecasts = Object.values(forecasts);
const averages = dailyForecasts.map((hourlyForecasts, idx) => {
const high = getTempAvg(hourlyForecasts, 'high');
const low = getTempAvg(hourlyForecasts, 'low');
const desc = getDescriptionAvg(hourlyForecasts);
const average = { date: Object.keys(forecasts)[idx], high, low, desc };
return average;
});
return averages;
}
我有一个函数,我试图通过一组对象进行映射。对于数组中的每个项目,我想计算某些平均值,然后将该数据弹出到不同的数组中。在函数的末尾,我想要 return 现在包含映射期间接收到的数据的数组。
相反,我return编辑了一个空数组。
我试图在不改变数组的情况下这样做,所以避免使用 Array.push() (我会注意到,如果我在我的函数中使用 Array.push 来更新我的数组,那么函数就可以工作).
这里是有问题的函数:
function getForecastAverages(forecasts) {
const dailyForecasts = Object.values(forecasts);
const averages = [];
dailyForecasts.map((hourlyForecasts, idx) => {
const high = getTempAvg(hourlyForecasts, 'high');
const low = getTempAvg(hourlyForecasts, 'low');
const desc = getDescriptionAvg(hourlyForecasts);
return [...averages, { date: Object.keys(forecasts)[idx], high, low, desc }];
});
return averages;
}
其他功能
function getTempAvg(forecasts, key) {
const avg = forecasts.reduce((acc, curr) => acc + curr[key], 0) / forecasts.length;
return avg.toFixed(2);
}
function getDescriptionAvg(forecasts) {
const length = forecasts.length;
const descriptions = forecasts.map(forecast => forecast.description);
return getMostFrequent(descriptions, length, 0);
}
function getMostFrequent(array, length, currIdx, maxCount = 0, mostFrequent = null, counts = {}) {
if (currIdx + 1 > length) return mostFrequent;
// first we need the word that we are counting the frequency of in the array
const word = array[currIdx];
// check if the current word exists in our counts object. If it doesn't, add the word and its count to the object. If it is present we increase its current count by 1
if (counts[word] === undefined) {
counts[word] = 1;
} else {
counts[word] = counts[word] + 1;
}
// check if the current word has a count that is larger than the highest count in our object. If it does we want to update our max count and most frequent word
if (counts[word] > maxCount) {
maxCount = counts[word];
mostFrequent = word;
}
const newIdx = currIdx + 1;
return getMostFrequent(array, length, newIdx, maxCount, mostFrequent, counts);
}
虚拟数据
forecasts = {
'2019-07-25': [
{
date: '2019-07-25 18:00:00',
description: 'broken clouds',
grnd_level: 1006.19,
high: 24.64,
humidity: 49,
low: 23.98,
pressure: 1017.03,
sea_level: 1017.03,
temp: 24.64,
},
{
date: '2019-07-25 21:00:00',
description: 'overcast clouds',
grnd_level: 1006.34,
high: 22.91,
humidity: 55,
low: 22.41,
pressure: 1017.16,
sea_level: 1017.16,
temp: 22.91,
},
],
'2019-07-26': [
{
date: '2019-07-26 18:00:00',
description: 'broken clouds',
grnd_level: 1006.19,
high: 24.64,
humidity: 49,
low: 23.98,
pressure: 1017.03,
sea_level: 1017.03,
temp: 24.64,
},
{
date: '2019-07-26 21:00:00',
description: 'overcast clouds',
grnd_level: 1006.34,
high: 22.91,
humidity: 55,
low: 22.41,
pressure: 1017.16,
sea_level: 1017.16,
temp: 22.91,
},
],
}
我也试过了
return averages.concat({ date: Object.keys(forecasts)[idx], high, low, desc })
收到错误消息 "averages" 是只读的,无法更改
const oldAvgs = averages
averages = [...oldAvgs, { date: Object.keys(forecasts)[idx], high, low, desc }]
return averages
有效,但正如我所说,它正在改变数组
return averages.push({ date: Object.keys(forecasts)[idx], high, low, desc })
您需要return映射对象
function getForecastAverages(forecasts) {
const dailyForecasts = Object.values(forecasts);
const dailyForecastKeys = Object.keys(forecasts);
const averages = dailyForecasts.map((hourlyForecasts, idx) => {
const high = getTempAvg(hourlyForecasts, 'high');
const low = getTempAvg(hourlyForecasts, 'low');
const desc = getDescriptionAvg(hourlyForecasts);
return { date: dailyForecastKeys[idx], high, low, desc };
});
return averages;
}
Javascript array map method returns 一个新的数组,你需要把你的代码稍微改成这样:
function getForecastAverages(forecasts) {
const dailyForecasts = Object.values(forecasts);
const averages = dailyForecasts.map((hourlyForecasts, idx) => {
const high = getTempAvg(hourlyForecasts, 'high');
const low = getTempAvg(hourlyForecasts, 'low');
const desc = getDescriptionAvg(hourlyForecasts);
const average = { date: Object.keys(forecasts)[idx], high, low, desc };
return average;
});
return averages;
}