简体   繁体   English

将reduce()方法转换为map()方法

[英]convert reduce() method to map() method

I would like to use the map() method for the _processData() function to extract the LATITUDE and LONGITUDE columns of my CSV file instead but not sure how. 我想对_processData()函数使用map()方法来提取CSV文件的LATITUDE和LONGITUDE列,但不确定如何。 I am using the d3-fetch to parse in CSV file. 我正在使用d3-fetch在CSV文件中进行解析。

  class App extends Component { state = { points: [], } componentDidMount() { this._processData() } _processData() { csv(csvFile).then(x => { const points = x.reduce((accu, curr) => { accu.push({ position: [Number(curr.LONGITUDE), Number(curr.LATITUDE)], pickup: true, }) return accu }, []) this.setState({ points, }) }) } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> 

You can simply do this: 您可以简单地做到这一点:

_processData() {
  csv(csvFile).then(x => {
    const points = x.map(curr => ({
      position: [Number(curr.LONGITUDE), Number(curr.LATITUDE)],
      pickup: true,
    }));
    this.setState({
      points,
    })
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM