2012-05-09 41 views
1

我有一個來自CrunchBase API的JSON對象,它提供了給定公司的一堆信息。現在我試圖通過JSON對象並創建他們的投資者列表。投資者可以分爲三類,"company", "financial_org", or "person"。所有這三種類型將被追加到同一個列表中,finalInvestorList從JSON對象的內部深度獲取信息

該腳本運行時沒有錯誤,但僅產生列出的第一輪投資者列表。我記錄了我認爲可能有用的一切。日誌處於同行註釋中。

基本上我的問題是,它只是循環一次,因此只是從第一輪增加投資者。任何幫助將不勝感激。讓我知道你是否需要更多信息!

var investorList = function(data, num) { 
     var fundingRounds = data["funding_rounds"]; 
     var finalInvestorList = [] 
     console.log(fundingRounds.length) // 3 


     for (i=0; i < fundingRounds.length; i++) { 
      var investments = data["funding_rounds"][i]["investments"]; 
      console.log(data["funding_rounds"][1]["investments"]); //correctly logs the index 1 round for spling (2nd round) 
      var round = data["funding_rounds"][i]; 
      console.log('round' + i); //only logs round0, never loops around for round1, round2 
      for (i=0; i < investments.length; i++) { 

       var angelObject = round["investments"][i]["person"]; 
       if (angelObject != null) { 
        console.log("angel fired"); //fires for "Mitch Blumenfeld" 
        var angel = angelObject["first_name"] + " " + angelObject["last_name"]; 
        finalInvestorList[i] = angel; 
       } 

       var financialOrgObject = round["investments"][i]["financial_org"]; 
       if (financialOrgObject != null) { 
        console.log("financial_org fired"); //fires for "Bucknell Venture Plan Competition" 
        console.log(financialOrgObject['name']); //Bucknell VPC 
        var financialOrg = financialOrgObject["name"] 
        finalInvestorList[i] = financialOrg 
       } 

       var companyObject = round['investments'][i]["company"]; 
       if (companyObject != null) { 
        console.log('company fired'); //i haven't bothered with this yet.. just logging it so ill know if its firing 
       } 
      } 
     } 
     console.log(finalInvestorList); //["Bucknell Venture Plan Competition", "Mitch Blumenfeld"] 

    } 

由該數據表示的JSON對象是如下 我與剛剛需要的比特縮短它。 JSON響應數據內的對象是由數據[「funding_rounds」] 這個數據是使用緊縮API檢索並且可以以完整形式在http://api.crunchbase.com/v/1/company/spling.js

"funding_rounds": 
    [{"round_code": "seed", 
    "source_url": "", 
    "source_description": "", 
    "raised_amount": 50000.0, 
    "raised_currency_code": "USD", 
    "funded_year": 2011, 
    "funded_month": 2, 
    "funded_day": 1, 
    "investments": 
    [{"company": null, 
     "financial_org": 
     {"name": "Bucknell Venture Plan Competition", 
     "permalink": "bucknell-venture-plan-competition", 
     "image": null}, 
     "person": null}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "Mitch", 
     "last_name": "Blumenfeld", 
     "permalink": "mitch-blumenfeld", 
     "image": null}}]}, 
    {"round_code": "seed", 
    "source_url": "http://techcrunch.com/2011/09/08/dreamit-ventures-launches-its-fall-2011-philadelphia-class/", 
    "source_description": "", 
    "raised_amount": 25000.0, 
    "raised_currency_code": "USD", 
    "funded_year": 2011, 
    "funded_month": 9, 
    "funded_day": 1, 
    "investments": 
    [{"company": null, 
     "financial_org": 
     {"name": "DreamIt Ventures", 
     "permalink": "dreamit-ventures", 
     "image": 
      {"available_sizes": 
      [[[150, 
       57], 
       "assets/images/resized/0002/7773/27773v5-max-150x150.jpg"], 
      [[250, 
       96], 
       "assets/images/resized/0002/7773/27773v5-max-250x250.jpg"], 
      [[251, 
       97], 
       "assets/images/resized/0002/7773/27773v5-max-450x450.jpg"]], 
      "attribution": null}}, 
     "person": null}]}, 
    {"round_code": "a", 
    "source_url": "http://techcrunch.com/2011/12/05/new-content-sharing-network-spling-launches-announces-400k-series-a/", 
    "source_description": "New Content Sharing Network Spling Launches, Announces $400K Series A", 
    "raised_amount": 400000.0, 
    "raised_currency_code": "USD", 
    "funded_year": 2011, 
    "funded_month": 12, 
    "funded_day": 5, 
    "investments": 
    [{"company": null, 
     "financial_org": 
     {"name": "Deep Fork Capital", 
     "permalink": "deep-fork-capital-2", 
     "image": 
      {"available_sizes": 
      [[[150, 
       20], 
       "assets/images/resized/0008/0167/80167v1-max-150x150.png"], 
      [[250, 
       34], 
       "assets/images/resized/0008/0167/80167v1-max-250x250.png"], 
      [[450, 
       62], 
       "assets/images/resized/0008/0167/80167v1-max-450x450.png"]], 
      "attribution": null}}, 
     "person": null}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "John", 
     "last_name": "Ason", 
     "permalink": "john-ason", 
     "image": null}}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "Mitchell", 
     "last_name": "Blumenfeld", 
     "permalink": "mitchell-blumenfeld", 
     "image": null}}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "Gianni", 
     "last_name": "Martire", 
     "permalink": "gianni-martire", 
     "image": 
      {"available_sizes": 
      [[[138, 
       150], 
       "assets/images/resized/0006/3720/63720v4-max-150x150.jpg"], 
      [[230, 
       250], 
       "assets/images/resized/0006/3720/63720v4-max-250x250.jpg"], 
      [[414, 
       450], 
       "assets/images/resized/0006/3720/63720v4-max-450x450.jpg"]], 
      "attribution": ""}}}]}] 

找到感謝您的幫助表示!

回答

3

您在每個循環中對計數器使用相同的變量名稱,因此當內循環完成並且外循環進入其第二次迭代時,iinvestments.length,而不是1。每個循環使用不同的變量名:

for (var roundIdx = 0; roundIdx < fundingRounds.length; roundIdx++) { 
    ... 
    for (var invIdx = 0; invIdx < investments.length; invIdx++) { 
     ... 

而且,不填充使用array[i] = value符號您的陣列,只需使用array.push(value)。您不需要跟蹤索引。

但是,我建議使用Array.forEach()迭代您的陣列和使用點語法,而不是方括號中:

function investorList(data, num) { 
    var finalInvestorList = []; 
    data.funding_rounds.forEach(function (round, i) { 
     round.investments.forEach(function (investment, i) { 
      if (investment.person) { 
       finalInvestorList.push(investment.person.first_name + " " + 
             investment.person.last_name); 
      } 
      else if (investment.financial_org) { 
       finalInvestorList.push(investment.financial_org.name) 
      } 
      else if (investment.company) { 
       finalInvestorList.push(investment.company.name) 
      } 
     } 
    } 
} 

對於舊版本瀏覽器本身不支持Array.forEach(),使用the implementation here勻場瀏覽器。

+0

完美謝謝。總是有助於在盯着它幾個小時之後再放上第二雙眼睛。謝謝! –