我不断收到 TypeError: countYear.filter 不是最后一行的函数,但我不明白为什么。我只想输出年份并计算(仅 2014 年)。这是原始代码的沙箱:https ://codesandbox.io/s/wizardly-clarke-szge3
const table = (data, e ) =>{
// get array of dates when bar is clicked
let newArr= data.datum.dimensions[0].datum.data.results.map((d) => d.Date)
console.log(newArr)
// convert into Date object
const datesArr = newArr.map(d => ({ date: new Date(d) , count : 0}))
console.log(datesArr)
// each year
const yearArr = datesArr.map((d) => ({year: d.date.getFullYear(), count: 0}))
console.log(yearArr)
const countYear = newArr.reduce((acc, date) => {
const years = new Date(date).getFullYear();
// acc = { date, count }
if (acc[years]) {
return {
...acc,
[years]: {
date: years,
count: acc[years]["count"] + 1
}
};
}
return {
...acc,
[years]: {
date: years,
count: 1
}
}
}
)
countYear.filter((e) => e.includes('2014'))
// setYear(Object.values(countYear))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>