根据一次匹配从json对象返回两个参数

时间:2019-04-13 20:06:04

标签: javascript json api

我正在尝试创建一个函数 oo(),该函数将从 JSON 对象 onclick 中提取适当的值以传递给 API 调用。我已经将数据组织到局部变量中,使用 hasOwnProperty 可以成功地遍历数据以返回与参数匹配的值。下一步是确定对象中这些参数内的键是否与参数 x 匹配,如果匹配,则返回它们的值和它们所属的委员会对象的id值。 (我觉得这里的术语不对了。)

让我尝试变得更加清晰。参考下面的数据,当通过功能传递小组委员会的名称时,我希望返回匹配参数的值以及它所属的委员会对象的ID。例如,如果oo('Elections')我需要访问id参数值“ HSHA”和“ HSHA01”或“ HSHA08”,然后将它们插入API URL,如下所示:

'https://api.propublica.org/congress/v1/116/senate/committees/HSHA/subcommittees/HSHA01.json'

这是我到目前为止尝试过的

//Converting for the ID of and Finding Committee & Subcommittee data
function oo(x) {

  var housecommittees = {
  "Committee on Administration":  {
    "id":  "HSHA",
    "subcommittees": {  
      "Elections": "HSHA01",
      "Elections": "HSHA08",
    },
  },

  "Committee on Agriculture": {
    "id":"HSAG",
    "subcommittees": {
      "Nutrition, Oversight, and Department Operations": "HSAG03",
      "General Farm Commodities and Risk Management": "HSAG16",
      "Conservation and Forestry": "HSAG15",
      "Commodity Exchanges, Energy, and Credit": "HSAG22",
      "Livestock and Foreign Agriculture": "HSAG29",
      "Biotechnology, Horticulture, and Research": "HSAG14",
    },
  },

  "Committee on Appropriations": {
    "id": "HSAP",
    "subcommittees": {
      "Agriculture, Rural Development, Food and Drug Administration, and Related Agencies": "HSAP01",
      "Commerce, Justice, Science, and Related Agencies": "HSAP19",
      "Defense": "HSAP02",
      "Energy and Water Development, and Related Agencies": "HSAP10",
      "Financial Services and General Government": "HSAP23",
      "Homeland Security": "HSAP15",
      "Interior, Environment, and Related Agencies": "HSAP06",
      "Labor, Health and Human Services, Education, and Related Agencies": "HSAP07",
      "Legislative Branch": "HSAP24",
      "Military Construction, Veterans Affairs, and Related Agencies": "HSAP18",
      "State, Foreign Operations, and Related Programs": "HSAP04",
      "Transportation, and Housing and Urban Development, and Related Agencies": "HSAP20",
    },
  },

  "Committee on Armed Services": {
    "id": "HSAS",
    "subcommittees": {
      "Tactical Air and Land Forces": "HSAS25",
      "Military Personnel": "HSAS02",
      "Readiness": "HSAS03",
      "Seapower and Projection Forces": "HSAS28",
      "Strategic Forces": "HSAS29",
      "Intelligence and Emerging Threats and Capabilities": "HSAS26",
    },
  },

  "Committee on Education and Labor": {
    "id": "HSED",
    "subcommittees": {
    "Early Childhood, Elementary, and Secondary Education": "HSED14",
    "Health, Employment, Labor, and Pensions": "HSED02",
    "Higher Education and Workforce Investment": "HSED13",
    "Workforce Protections": "HSED10",
    "Civil Rights and Human Services": "HSED07",
    },
  },

  "Committee on Energy and Commerce": {
    "id": "HSIF",
    "subcommittees": {
      "Consumer Protection and Commerce": "HSIF17",
      "Communications and Technology": "HSIF16",
      "Energy": "HSIF03",
      "Health": "HSIF14",
      "Oversight and Investigations": "HSIF02",
      "Environment and Climate Change": "HSIF18",
    },
  },

  "Committee on Ethics": {
    "id": "HSSO",
    "subcommittees": {

    },
  },

  "Committee on Financial Services": {
    "id": "HSBA",
    "subcommittees": {
      "Investor Protection, Entrepreneurship, and Capital Markets": "HSBA16",
      "Consumer Protection and Financial Institutions": "HSBA15",
      "Housing, Community Development, and Insurance": "HSBA04",
      "Oversight and Investigations": "HSBA09",
      "National Security, International Development, and Monetary Policy": "HSBA01",
      "Diversity and Inclusion": "HSBA13",
    },
  },

  "Committee on Foreign Affairs": {
    "id": "HSFA",
    "subcommittees": {
      "Africa, Global Health, Global Human Rights, and International Organizations": "HSFA16",
      "Asia, the Pacific, and Nonproliferation": "HSFA05",
      "Europe, Eurasia, Energy, and the Environment": "HSFA14",
      "Oversight and Investigations": "HSFA17",
      "Middle East, North Africa, and International Terrorism": "HSFA13",
      "Western Hemisphere, Civilian Security, and Trade": "HSFA07",
    },
  },

  "Committee on Homeland Security": {
    "id": "HSHM",
    "subcommittees": {
      "Border Security, Facilitation, and Operations": "HSHM11",
      "Emergency Preparedness, Response, and Recovery": "HSHM12",
      "Cybersecurity, Infrastructure Protection, and Innovation": "HSHM08",
      "Intelligence and Counterterrorism": "HSHM05",
      "Oversight, Management, and Accountability": "HSHM09",
      "Transportation and Maritime Security": "HSHM07",
    },
  },

  "Committee on Natural Resources": {
    "id": "HSII",
    "subcommittees": {
      "Energy and Mineral Resources": "HSII06",
      "Fisheries, Wildlife, Oceans and Insular Affairs": "HSII22",
      "National Parks, Forests, and Public Lands": "HSII10",
      "Water, Oceans, and Wildlife": "HSII13",
      "Oversight and Investigations": "HSII15",
      "Indigenous Peoples of the United States": "HSII24",
    },
  },

  "Committee on Oversight and Reform": {
    "id": "HSGO",
    "subcommittees": {
      "Information Technology": "HSGO25",
      "Government Operations": "HSGO24",
      "National Security": "HSGO06",
      "Environment": "HSGO28",
      "Healthcare, Benefits, and Administrative Rules": "HSGO27",
      "Intergovernmental Affairs": "HSGO29",
      "Civil Rights and Civil Liberties": "HSGO02",
      "Economic and Consumer Policy": "HSGO05",
    },
  },

  "Committee on Rules": {
    "id": "HSRU",
    "subcommittees": {
      "Legislative and Budget Process": "HSRU02",
      "Rules and Organization of the House": "HSRU04",
      "Expedited Procedures": "HSRU05",
    },
  },

  "Committee on Science, Space, and Technology": {
    "id": "HSSY",
    "subcommittees": {
      "Energy": "HSSY20",
      "Investigations and Oversight": "HSSY21",
      "Space and Aeronautics": "HSSY16",
      "Research and Technology": "HSSY15",
      "Environment": "HSSY18",
    },
  },

  "Committee on Small Business": {
    "id": "HSSM",
    "subcommittees": {
      "Contracting and Infrastructure": "HSSM23",
      "Investigations, Oversight and Regulations": "HSSM24",
      "Rural Development, Agriculture, Trade and Entrepreneurship": "HSSM25",
      "Economic Growth, Tax and Capital Access": "HSSM27",
      "Innovation and Workforce Development": "HSSM26",
    },
  },

  "Committee on the Budget": {
    "id": "HSBU",
    "subcommittees": {
    },
  },
  "Committee on the Judiciary": {
    "id": "HSJU",
    "subcommittees": {
      "Constitution, Civil Rights, and Civil Justice": "HSJU10",
      "Courts, Intellectual Property, and the Internet": "HSJU03",
      "Crime, Terrorism and Homeland Security": "HSJU08",
      "Immigration and Citizenship": "HSJU01",
      "Antitrust, Commercial and Administrative Law": "HSJU05",
    },
  },

  "Committee on Transportation and Infrastructure": {
    "id": "HSPW",
    "subcommittees": {
      "Aviation": "HSPW05",
      "Coast Guard and Maritime Transportation": "HSPW07",
      "Economic Development, Public Buildings, and Emergency Management": "HSPW13",
      "Highways and Transit": "HSPW12",
      "Railroads, Pipelines, and Hazardous Materials": "HSPW14",
      "Water Resources and Environment": "HSPW02",
    },
  },

  "Committee on Veterans' Affairs": {
    "id": "HSVR",
    "subcommittees": {
      "Disability Assistance and Memorial Affairs": "HSVR09",
      "Economic Opportunity": "HSVR10",
      "Health": "HSVR03",
      "Oversight and Investigations": "HSVR08",
      "Technology Modernization": "HSVR11",
    },
  },

  "Committee on Ways and Means": {
    "id": "HSWM",
    "subcommittees": {
      "Health": "HSWM02",
      "Worker and Family Support": "HSWM03",
      "Oversight": "HSWM06",
      "Select Revenue Measures": "HSWM05",
      "Social Security": "HSWM01",
      "Trade": "HSWM04",
    },
  },

  "Permanent Select Committee on Intelligence": {
    "id": "HSIG",
    "subcommittees": {
      "Defense Intelligence and Warfighter Support": "HSIG06",
      "Oversight and Investigations": "HSIG09",
      "Counterterrorism, Counterintelligence, and Counterproliferation": "HSIG05",
      "Intelligence Modernization and Readiness": "HSIG08",
      "Department of Defense Intelligence and Overhead Architecture": "HSIG04",
      "Emerging Threats": "HSIG03",
      "National Security Agency and Cybersecurity": "HSIG02",
      "Central Intelligence Agency": "HSIG01",
      "Strategic Technologies and Advanced Research": "HSIG10",
    },
  },
  "Select Committee on the Climate Crisis": {
    "id": "HSCN",
    "subcommittees": {
    },
  },
  "Select Committee on the Modernization of Congress": {
    "id": "HSMH",
    "subcommittees": {

    },
  },
};

  var senatecommittees = {
  "Committee on Agriculture, Nutrition, and Forestry": {
      "id": "SSAF",
      "subcommittees": {
        "Commodities, Risk Management, and Trade": "SSAF13",
        "Conservation, Forestry, and Natural Resources": "SSAF14",
        "Rural Development and Energy": "SSAF15",
        "Livestock, Marketing, and Agriculture Security": "SSAF17",
        "Nutrition, Agricultural Research, and Specialty Crops": "SSAF16",
    },
  },
  "Committee on Appropriations": {
    "id": "SSAP",
    "subcommittees": {
      "Departments of Labor, Health and Human Services, and Education, and Related Agencies": "SSAP18",
      "Commerce, Justice, Science, and Related Agencies Subcommittee": "SSAP16",
      "Energy and Water Development Subcommittee": "SSAP22",
      "State, Foreign Operations, and Related Programs": "SSAP20",
      "Department of Homeland Security Subcommittee": "SSAP14",
      "Department of the Interior, Environment, and Related Agencies": "SSAP17",
      "Transportation, Housing and Urban Development, and Related Agencies": "SSAP24",
      "Legislative Branch Subcommittee": "SSAP08",
      "Agriculture, Rural Development, Food and Drug Administration, and Related Agencies": "SSAP01",
      "Department of Defense Subcommittee": "SSAP02",
      "Military Construction and Veterans Affairs, and Related Agencies": "SSAP19",
      "Financial Services and General Government": "SSAP23",
    },
  },
  "Committee on Armed Services": {
    "id":"SSAS",
    "subcommittees": {
      "Strategic Forces Subcommittee": "SSAS16",
      "Emerging Threats and Capabilities": "SSAS20",
      "Personnel": "SSAS17",
      "SeaPower Subcommittee": "SSAS13",
      "Airland Subcommittee": "SSAS14",
      "Readiness and Management Support": "SSAS15",
      "Cybersecurity": "SSAS21",
    },
  },
  "Committee on Banking, Housing, and Urban Affairs": {
    "id":"SSBK",
    "subcommittees": {
      "Securities, Insurance, and Investment": "SSBK04",
      "Housing, Transportation, and Community Development": "SSBK09",
      "Economic Policy": "SSBK12",
      "National Security and International Trade and Finance": "SSBK05",
      "Financial Institutions and Consumer Protection": "SSBK08",
    },
  },
  "Committee on Commerce, Science, and Transportation": {
    "id": "SSCM",
    "subcommittees": {
      "Communications, Technology, Innovation, and the Internet": "SSCM26",
      "Transportation and Safety": "SSCM32",
      "Security Subcommittee": "SSCM31",
      "Manufacturing, Trade, and Consumer Protection": "SSCM29",
      "Aviation and Space Subcommittee": "SSCM28",
      "Science, Oceans, Fisheries, and Weather": "SSCM30",
    },
  },
  "Committee on Energy and Natural Resources": {
    "id":"SSEG",
    "subcommittees": {
      "Public Lands, Forests, and Mining": "SSEG03",
      "National Parks": "SSEG04",
      "Water and Power Subcommittee": "SSEG07",
      "Energy": "SSEG01",
    },
  },
  "Committee on Environment and Public Works": {
    "id":"SSEV",
    "subcommittees": {
      "Fisheries, Water, and Wildlife": "SSEV15",
      "Superfund, Waste Management, and Regulatory Oversight": "SSEV09",
      "Clean Air and Nuclear Safety": "SSEV10",
      "Green Jobs and the New Economy": "SSEV16",
      "Transportation and Infrastructure": "SSEV08",
      "Oversight": "SSEV18",
    },
  },
  "Committee on Finance": {
    "id": "SSFI",
    "subcommittees": {
      "International Trade, Customs, and Global Competitiveness": "SSFI13",
      "Energy, Natural Resources, and Infrastructure": "SSFI12",
      "Fiscal Responsibility and Economic Growth": "SSFI14",
      "Health Care": "SSFI10",
      "Social Security, Pensions, and Family Policy": "SSFI02",
      "Taxation and IRS Oversight": "SSFI11",
    },
  },
  "Committee on Foreign Relations": {
  "id": "SSFR",
  "subcommittees": {
    "International Development and Foreign Assistance, Economic Affairs, International Environmental Protection, and Peace Corps": "SSFR12",
    "International Operations and Organizations, Human Rights, Democracy, and Global Women's Issues": "SSFR13",
    "Africa and Global Health Policy: SSFR09 Europe and Regional Security Cooperation": "SSFR01",
    "East Asia, the Pacific, and International Cybersecurity Policy": "SSFR02",
    "Near East, South Asia, Central Asia, and Counterterrorism": "SSFR07",
    "Western Hemisphere, Transnational Crime, Civilian Security, Democracy, Human Rights, and Global Women's Issues": "SSFR06",
    "State Department and USAID Management, International Operations, and Bilateral International Development": "SSFR14",
    "Multilateral International Development, Multilateral Institutions, and International Economic, Energy, and Environmental Policy": "SSFR15",
    },
  },
  "Committee on Health, Education, Labor, and Pensions": {
    "id":"SSHR",
    "subcommittees": {
      "Children and Families: SSHR09 Employment and Workplace Safety": "SSHR11",
      "Primary Health and Retirement Security": "SSHR12",
    },
  },
  "Committee on Homeland Security and Governmental Affairs": {
    "id":"SSGA",
    "subcommittees": {
      "Permanent Subcommittee on Investigations": "SSGA01",
      "Financial and Contracting Oversight": "SSGA15",
      "Emergency Management, Intergovernmental Relations, and the District of Columbia": "SSGA17",
      "the Efficiency and Effectiveness of Federal Programs and the Federal Workforce": "SSGA16",
      "Regulatory Affairs and Federal Management": "SSGA19",
      "Federal Spending Oversight and Emergency Management": "SSGA18",
    },
  },
  "Committee on Indian Affairs": {
    "id": "SLIA",
  },

  "Committee on Rules and Administration": {
    "id": "SSRA",
  },

  "Committee on Small Business and Entrepreneurship": {
    "id": "SSSB",
  },

  "Committee on the Budget": {
    "id": "SSBU",
  },

  "Committee on the Judiciary": {
    "id": "SSJU",
  },

  "Committee on Veterans' Affairs": {
    "id": "SSVA",
  },

  "Select Committee on Ethics": {
    "id": "SLET",
  },

  "Select Committee on Intelligence": {
    "id": "SLIN",
  },

  "Special Committee on Aging": {
    "id": "SPAG",
  },

  "United States Caucus on International Narcotics Control": {
    "id": "SCNC",
  },
};

    if (senatecommittees.hasOwnProperty(x)) {
      console.log('Senate Committee match for '+ x + ': '+senatecommittees[x].id );
    } else {
      console.log('Not a Senate Committee');
      for (h=0; h<=senatecommittees.length; h++) {
        for (y=o; y<=senatecommittees.subcommittees; y++) {
          if (subcommittees[y].hasOwnProperty(x)) {
            console.log('Match found for senate subcommittee '+senatecommittees[h]+ ': '+senatecommittees[h].subcommittees[x] );
            $.ajax({
              url: 'https://api.propublica.org/congress/v1/116/senate/committees/'+senatecommittees[h].id+'/subcommittees/'+subcommittees[y]+'.json',
              type: 'GET',
              dataType: 'json',
              headers: {'X-API-Key': 'xxx'}

            })

          } else {
            console.log('Not a senate subcommittee');
          }
        }
      }
    } 
    if (housecommittees.hasOwnProperty(x)) {
      console.log('House Committee match for '+x+': '+housecommittees[x].id);
    } else {
      console.log('Not a House Committee');
    }

};

我已将API调用包含在函数内,只是为了提供希望的结果。通过该功能传递小组委员会的名称时,我现在登录到控制台的所有内容都是“不是参议院委员会”和“不是众议院委员会”

0 个答案:

没有答案