Javascript 为什么这个for循环运行了这么多次

Javascript 为什么这个for循环运行了这么多次,javascript,reactjs,api,Javascript,Reactjs,Api,下面是我正在处理的一个组件的代码。它需要一个股票列表,并使用FinancialModelingPrep的API,它可以得到我的历史月末价格。我正在使用getPrice函数中的console.log来查看问题所在,因为它会导致我在一次调用中达到API限制,而每次调用只能进行15次调用。我想知道我的逻辑是否有问题,因为console.log会将阵列中的每个股票显示100次左右。这方面的任何帮助都会很好 import React, { Component } from "react"

下面是我正在处理的一个组件的代码。它需要一个股票列表,并使用FinancialModelingPrep的API,它可以得到我的历史月末价格。我正在使用getPrice函数中的console.log来查看问题所在,因为它会导致我在一次调用中达到API限制,而每次调用只能进行15次调用。我想知道我的逻辑是否有问题,因为console.log会将阵列中的每个股票显示100次左右。这方面的任何帮助都会很好

import React, { Component } from "react";
import axios from 'axios';
import apiKey from '../../config.js'


class Holdings extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      data: props.list
    }
    this.getNames = this.getNames.bind(this)
    this.getPrice = this.getPrice.bind(this)
  }

  getNames(obj) {
    let last = obj[Object.keys(obj)[Object.keys(obj).length - 1]]
    for (let i = 0; i < last.length; i++) {
      this.getPrice(last[i])
      console.log(last[i])
    }
    return last
  }

  getPrice(name) {
    let year = new Date().getFullYear()
    let month = new Date().getMonth()
    let prevMonthEnd = {
      0: year - 1 + '-12-31',
      1: year + '-01-31',
      2: year + '-02-28',
      3: year + '-03-31',
      4: year + '-04-30',
      5: year + '-05-31',
      6: year + '-06-30',
      7: year + '-07-31',
      8: year + '-08-31',
      9: year + '-09-30',
      10: year + '-10-31',
      11: year + '-11-30',
    }
    let ticker = name['Ticker'].split(' ')[0]
    axios.get(`https://financialmodelingprep.com/api/v3/historical-price-full/${ticker}?from=${prevMonthEnd[month]}&to=${prevMonthEnd[month]}&apikey=${apiKey['TOKEN']}`)
      .then(res => {
        name['Beg Month Price'] = res.data['historical'][0]['close']
      })
      .then(() => console.log('New Stock: ', name))
      .catch(error => console.log('error with API: ', error))
  }


  render() {
    return (
      <div>
        <h3>Current Portfolio Holdings</h3>
        <table className="table table-striped table-sm">
          <tbody>
            <tr>
              <td>Ticker</td>
              <td>Name</td>
              <td>MTD Return</td>
            </tr>
            {this.getNames(this.state.data).map((i, key) => {
              return <tr key={key++}>
                <td>{i['Ticker'].split(' ')[0]}</td>
                <td>{i['Name']}</td>
                <td>{i['Beg Month Price']}</td>
              </tr>
            })}
          </tbody>
        </table>
      </div>
    )
  }
}


import React,{Component}来自“React”;
从“axios”导入axios;
从“../../config.js”导入apiKey
类。组件{
建造师(道具){
超级(道具)
此.state={
数据:道具列表
}
this.getNames=this.getNames.bind(this)
this.getPrice=this.getPrice.bind(this)
}
getNames(obj){
让last=obj[Object.keys(obj)[Object.keys(obj.length-1]]
for(设i=0;i{
name['Beg Month Price']=res.data['historical'][0]['close']
})
.then(()=>console.log('newstock:',name))
.catch(error=>console.log('error with API:',error))
}
render(){
返回(
当前投资组合持有量
股票行情
名称
MTD返回
{this.getNames(this.state.data).map((i,key)=>{
返回
{i['Ticker'].split('')[0]}
{i['Name']}
{i['Beg Month Price']}
})}
)
}
}

这是否可能是包含应用程序中发生重新提交的问题?您可以检查componentdiddupdate以验证这是否是原因。

我觉得错误在于
let last=obj[Object.keys(obj)[Object.keys(obj.length-1]
。您是否能够共享一个样本obj(股票列表)?另外,为什么您在API请求中有相同的from和to,位于
?from=${prevMonthEnd[month]}&to=${prevMonthEnd[month]}
?当然。下面是一个它看起来像什么的例子。这是一个由15个组成的数组:{1M回报率:“0”2M回报率:“0”3M回报率:“0”流动比率:“1.719715808”债务/权益比率:“158.6905949”部门收益率:“0.8153”收益率:“3.3822”自由现金流增长率:“9.211694885”GICS部门:“金融”市值:“大型”名称:“标准普尔全球公司”营业利润率:“51.69630643”营业利润率增长:“10.55258074”ROA:“24.98151571”排名:41.833336排名:(6)[50,1,89,32,0,79]股票代码:“SPGI美国股票”}对于你的第二个问题,这就是终点的方式。我只需要它在过去的一个特定的一天,所以我必须使“从”和“到”部分作为解决办法的同一天。