使用 reduce 内部地图
Using reduce inside map
每当我想在 map 函数中使用 reduce 时,我都会收到错误 'reduce is not a function'。
我有一个数组,我在其中通过 2 个键进行映射。日期和金额。下面是带有对象的数组。
const dividendArrayFiltered = [
{
"date": "2021-11-09",
"amount": 0.5
},
{
"date": "2021-11-08",
"amount": 0.5
},
{
"date": "2021-11-08",
"amount": 0.5
},
{
"date": "2021-11-11",
"amount": 1
},
{
"date": "2021-11-11",
"amount": 1
}
]
在此地图中,我想减少具有相同日期的值。
这是出错的代码。不太清楚为什么 reduce 函数不起作用。希望你能帮帮我。谢谢!
const summedDividend = dividendArrayFiltered.map(({ date, amount }) => {
const grouped = date.reduce(
(a, d, i) => a.set(d, (a.get(d) ?? 0) + amount[i]),
new Map()
);
return {
date: [...grouped.keys()],
amount: [...grouped.values()],
};
});
您自己的解决方案不起作用,因为在 map 函数中您只能访问当前项,不能访问数组,而 reduce 函数只能用于数组。
备选方案:
您可以创建一个空数组,在其中仅推送原始数组中的这些项目,这些项目的日期与新数组中的项目之一不同。
let uniqueArray = []
// iterate through all array elements
dividendArrayFiltered.forEach((item) => {
// check if an element with the current item date exists in your new uniqueArray and which position index it has
const itemPos = uniqueArray.findIndex(uniqueItem => uniqueItem.date == item.date)
// if the index is negative, it's not included in the array, so you push your item to the new array
if(itemPos < 0) {
uniqueArray.push(item)
}
// if it's included, you take the old amount and add your new value
else {
uniqueArray[itemPos].amount = uniqueArray[itemPos].amount + item.amount;
}
});
console.log(uniqueArray);
在 uniqueArray
中现在只有唯一的日期项。
可以直接对原数组进行归约,也可以利用findIndex()
:
const dividendArrayFiltered = [
{ date: '2021-11-09', amount: 0.5 },
{ date: '2021-11-08', amount: 0.5 },
{ date: '2021-11-08', amount: 0.5 },
{ date: '2021-11-11', amount: 1 },
{ date: '2021-11-11', amount: 1 }
]
const grouped = dividendArrayFiltered.reduce((a, c) => {
const dateIndex = a.findIndex(o => o.date === c.date)
if (dateIndex !== -1) a[dateIndex].amount += c.amount
else a.push({ date: c.date, amount: c.amount })
return a
}, [])
console.log(grouped)
.as-console-wrapper { min-height: 100% }
每当我想在 map 函数中使用 reduce 时,我都会收到错误 'reduce is not a function'。
我有一个数组,我在其中通过 2 个键进行映射。日期和金额。下面是带有对象的数组。
const dividendArrayFiltered = [
{
"date": "2021-11-09",
"amount": 0.5
},
{
"date": "2021-11-08",
"amount": 0.5
},
{
"date": "2021-11-08",
"amount": 0.5
},
{
"date": "2021-11-11",
"amount": 1
},
{
"date": "2021-11-11",
"amount": 1
}
]
在此地图中,我想减少具有相同日期的值。
这是出错的代码。不太清楚为什么 reduce 函数不起作用。希望你能帮帮我。谢谢!
const summedDividend = dividendArrayFiltered.map(({ date, amount }) => {
const grouped = date.reduce(
(a, d, i) => a.set(d, (a.get(d) ?? 0) + amount[i]),
new Map()
);
return {
date: [...grouped.keys()],
amount: [...grouped.values()],
};
});
您自己的解决方案不起作用,因为在 map 函数中您只能访问当前项,不能访问数组,而 reduce 函数只能用于数组。
备选方案:
您可以创建一个空数组,在其中仅推送原始数组中的这些项目,这些项目的日期与新数组中的项目之一不同。
let uniqueArray = []
// iterate through all array elements
dividendArrayFiltered.forEach((item) => {
// check if an element with the current item date exists in your new uniqueArray and which position index it has
const itemPos = uniqueArray.findIndex(uniqueItem => uniqueItem.date == item.date)
// if the index is negative, it's not included in the array, so you push your item to the new array
if(itemPos < 0) {
uniqueArray.push(item)
}
// if it's included, you take the old amount and add your new value
else {
uniqueArray[itemPos].amount = uniqueArray[itemPos].amount + item.amount;
}
});
console.log(uniqueArray);
在 uniqueArray
中现在只有唯一的日期项。
可以直接对原数组进行归约,也可以利用findIndex()
:
const dividendArrayFiltered = [
{ date: '2021-11-09', amount: 0.5 },
{ date: '2021-11-08', amount: 0.5 },
{ date: '2021-11-08', amount: 0.5 },
{ date: '2021-11-11', amount: 1 },
{ date: '2021-11-11', amount: 1 }
]
const grouped = dividendArrayFiltered.reduce((a, c) => {
const dateIndex = a.findIndex(o => o.date === c.date)
if (dateIndex !== -1) a[dateIndex].amount += c.amount
else a.push({ date: c.date, amount: c.amount })
return a
}, [])
console.log(grouped)
.as-console-wrapper { min-height: 100% }