问题描述
我不断收到 TypeError: countYear.filter is not a function for the last line 但我不明白为什么。我只想输出年份并计算(仅 2014 年)。这是原始代码的沙箱:https://codesandbox.io/s/wizardly-clarke-szge3
hystrix:
command:
default:
circuitBreaker:
enabled: true
requestVolumeThreshold: 10
sleepWindowInMilliseconds: 20000
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 4000
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))
}
解决方法
因为 countYear
是一个对象,并且该对象没有方法过滤器。