【问题标题】:getting information from the inner depths of a JSON object从 JSON 对象的内部深处获取信息
【发布时间】:2012-05-18 11:33:06
【问题描述】:

我有一个来自 CrunchBase API 的 JSON 对象,它为我提供了来自给定公司的大量信息。现在我正在尝试通过 JSON 对象并创建他们的投资者列表。投资者可以属于以下三类之一,"company", "financial_org", or "person"。所有三种类型都将附加到同一个列表中,finalInvestorList

脚本运行没有错误,但只生成第一轮上市的投资者名单。我已经记录了我认为可能有帮助的所有内容。日志在同一行 cmets 中。

基本上我的问题是它只循环一次,因此只添加第一轮的投资者。任何帮助将不胜感激。如果您需要更多信息,请告诉我!

  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 响应数据中的对象由 data["funding_rounds"] 表示 此数据是使用 crunch 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": ""}}}]}]

感谢您的帮助!

【问题讨论】:

    标签: javascript json object loops for-loop


    【解决方案1】:

    您在每个循环中为计数器使用相同的变量名称,因此当内循环完成并且外循环进行第二次迭代时,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 填充浏览器。

    【讨论】:

    • 非常感谢。盯着某样东西看了好几个小时后,总能帮助你把第二双眼睛放在上面。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多