最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I am getting TypeError saying forEach is not a function? - Stack Overflow

programmeradmin3浏览0评论

I am trying to display Linechart using chartist.js. I have used reduce() to extract data from props. I have keys as year and values as average value in averages object. But I am not able to display the line chart as I am not able to see any lines on chart I am getting type error. I have created React ponent and inside ponentDidMount() I have added my line chart code. I think the issue is related to javascript/reactjs and not chartist.js

Code:

class Linechart extends Component {

  constructor(props){
    super(props);
  }

  ponentDidMount(){

        const totals = this.props.bowldata.reduce((a, {season, econ})  => {

          if (!a[season]) {
            a[season] = {sum: 0, count: 0};
          }

          if(econ !== null){
            a[season].count++;
            a[season].sum += parseInt(econ);

          }

          return a; 

      }, {});

        const averages = {};

        Object.keys(totals).forEach(key => {   

          averages[key] = totals[key].sum / totals[key].count;

        });

      console.log(averages);

      const series = Object.values(averages);

      const labels = Object.keys(averages);

        const data = {
            series:series,
            labels:labels
        }

        this.linechart = new Chartist.Line('.ct-line-chart', data, options);
  }

  render(){
    return(
      <div>
          <div className="col-md-3 col-xs-6 ct-line-chart">
              {this.linechart}
          </div>
        </div>
    )}
}

export default Linechart ;

Using .forEach() I am trying to create new array of objects with sum/count as a value and key as year.

Screenshot:

I am trying to display Linechart using chartist.js. I have used reduce() to extract data from props. I have keys as year and values as average value in averages object. But I am not able to display the line chart as I am not able to see any lines on chart I am getting type error. I have created React ponent and inside ponentDidMount() I have added my line chart code. I think the issue is related to javascript/reactjs and not chartist.js

Code:

class Linechart extends Component {

  constructor(props){
    super(props);
  }

  ponentDidMount(){

        const totals = this.props.bowldata.reduce((a, {season, econ})  => {

          if (!a[season]) {
            a[season] = {sum: 0, count: 0};
          }

          if(econ !== null){
            a[season].count++;
            a[season].sum += parseInt(econ);

          }

          return a; 

      }, {});

        const averages = {};

        Object.keys(totals).forEach(key => {   

          averages[key] = totals[key].sum / totals[key].count;

        });

      console.log(averages);

      const series = Object.values(averages);

      const labels = Object.keys(averages);

        const data = {
            series:series,
            labels:labels
        }

        this.linechart = new Chartist.Line('.ct-line-chart', data, options);
  }

  render(){
    return(
      <div>
          <div className="col-md-3 col-xs-6 ct-line-chart">
              {this.linechart}
          </div>
        </div>
    )}
}

export default Linechart ;

Using .forEach() I am trying to create new array of objects with sum/count as a value and key as year.

Screenshot:

Share Improve this question edited Jun 24, 2018 at 9:36 stone rock asked Jun 24, 2018 at 9:14 stone rockstone rock 1,95310 gold badges45 silver badges76 bronze badges 3
  • 3 The line indicated in the OP is not the line giving the error that is in the console image, which is something like data.normalized[seriesIndex].forEach is not a function. – RobG Commented Jun 24, 2018 at 9:26
  • @RobG How can I find average ? I want sum/count as a value and key as year. – stone rock Commented Jun 24, 2018 at 9:32
  • @RobG I have edited my question please see it. – stone rock Commented Jun 24, 2018 at 9:37
Add a ment  | 

1 Answer 1

Reset to default 11

Change your code like this see if the problem is gone. In docs it shows series should be a multidimensional array.

 const data = {
     series:[series],
     labels:labels
 }
发布评论

评论列表(0)

  1. 暂无评论