filter array method not working on reduce method

135 views Asked by At

i keep getting TypeError: countYear.filter is not a function for the last line but i dont understand why. I want to output just the years and count (only the 2014 year). heres the sandbox for the original code: 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>

1

There are 1 answers

1
Vulwsztyn On

Because countYear is an object, and the object has no method filter.