javascript - 如何从所有功能中返回单个字段?

标签 javascript json node.js

我需要返回单个字段的所有特征,并且只返回该单个字段,而不是整个数据集。

我的 json 文件格式如下:

{"type":"FeatureCollection","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features":[{"type":"Feature","properties":{"NAME":"Kingston upon Thames","GSS_CODE":"E09000021","HECTARES":3726.117,"NONLD_AREA":0,"ONS_INNER":"F","SUB_2009":null,"SUB_2006":null,"Area_name":"Kingston upon Thames","Inner_Outer_London":"Outer London","GLA_Population_Estimate_2015":170900,"GLA_Household_Estimate_2015":66870.... ,

{"type":"Feature","properties":{"NAME":"Croydon","GSS_CODE":"E09000008","HECTARES":8649.441,"NONLD_AREA":0,"ONS_INNER":"F","SUB_2009":null,"SUB_2006":null,"Area_name":"Croydon","Inner_Outer_London":"Outer London","GLA_Population_Estimate_2015":380700,"GLA_Household_Estimate_2015":152750,"Inland_Area_Hectares":8650.4,"Population_density_per_hectare_2015":44,"Average_Age_2015":36.8,"Proportion_of_population_aged_0-15_2015":22.1...

编辑:json 到 json lint(太大甚至无法复制粘贴其中的 5%)

{
"type": "FeatureCollection",
"crs": {
    "type": "name",
    "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
},
"features": [{
    "type": "Feature",
    "properties": {
        "NAME": "Kingston upon Thames",
        "GSS_CODE": "E09000021",
        "HECTARES": 3726.117,
        "NONLD_AREA": 0,
        "ONS_INNER": "F",
        "SUB_2009": null,
        "SUB_2006": null,
        "Area_name": "Kingston upon Thames",
        "Inner_Outer_London": "Outer London",
        "GLA_Population_Estimate_2015": 170900,
        "GLA_Household_Estimate_2015": 66870,
        "Inland_Area_Hectares": 3726.1,
        "Population_density_per_hectare_2015": 45.9,
        "Average_Age_2015": 36.9,
        "Proportion_of_population_aged_0-15_2015": 19,
        "Proportion_of_population_of_working-age_2015": 68.1,
        "Proportion_of_population_aged_65_and_over_2015": 12.9,
        "Net_internal_migration_2014": -188,
        "Net_international_migration_2014": 2136,
        "Net_natural_change_2014": 1178,
        "PC_of_resident_population_born_abroad_2014": 29.2,
        "Largest_migrant_population_by_country_of_birth_2011": "Sri Lanka",
        "PC_of_largest_migrant_population_2011": 2.2,
        "Second_largest_migrant_population_by_country_of_birth_2011": "India",
        "PC_of_second_largest_migrant_population_2011": 1.8,
        "Third_largest_migrant_population_by_country_of_birth_2011": "Korea (South)",
        "PC_of_third_largest_migrant_population_2011": 1,
        "PC_of_population_from_BAME_groups_2013": 27,
        "PC_people_aged_3_whose_main_language_is_not_English_2011_Census": 16.4,
        "Overseas_nationals_entering_the_UK_NINo_2014_15": 3157,
        "New_migrant_NINo_rates_2014_15": 27.6,
        "Largest_migrant_population_arrived_during_2014_15": "Bulgaria",
        "Second_largest_migrant_population_arrived_during_2014_15": "Poland",
        "Third_largest_migrant_population_arrived_during_2014_15": "Romania",
        "Employment_rate_PC_2014": 75,
        "Male_employment_rate_2014": 81.6,
        "Female_employment_rate_2014": 68.5,
        "Unemployment_rate_2014": 5.3,
        "Youth_Unemployment_claimant_rate_18-24_Dec-14": 3,
        "Proportion_of_16-18_year_olds_who_are_NEET_PC_2014": 3.9,
        "Proportion_of_the_working-age_population_who_claim_out-of-work_benefits_PC_May-2014": 7,
        "PC_working-age_with_a_disability_2014": 13.8,
        "Proportion_of_working_age_people_with_no_qualifications_PC_2014": 3,
        "Proportion_of_working_age_with_degree_or_equivalent_and_above_PC_2014": 57.8,
        "Gross_Annual_Pay_2014": 36700,
        "Gross_Annual_Pay_-_Male_2014": 39359,
        "Gross_Annual_Pay_-_Female_2014": 30144,
        "Modelled_Household_median_income_estimates_2012_13": 56920,
        "PC_adults_that_volunteered_in_past_12_months_2010_11_to_2012_13": 42.7,
        "Number_of_jobs_by_workplace_2013": 82300,
        "PC_of_employment_that_is_in_public_sector_2013": 17.8,
        "Jobs_Density_2013": 1,
        "Number_of_active_businesses_2013": 8130,
        "Two-year_business_survival_rates_started_in_2011": 79,
        "Crime_rates_per_thousand_population_2014_15": 58.5,
        "Fires_per_thousand_population_2014": 1.8,
        "Ambulance_incidents_per_hundred_population_2014": 11.1,
        "Median_House_Price_2014": 385000,
        "Average_Band_D_Council_Tax_charge_2015_16": 1674.7,
        "New_Homes_net_2013_14": 260,
        "Homes_Owned_outright_2014_PC": 27.7,
        "Being_bought_with_mortgage_or_loan_2014_PC": 39,
        "Rented_from_Local_Authority_or_Housing_Association_2014_PC": 10.7,
        "Rented_from_Private_landlord_2014_PC": 22.4,
        "PC_of_area_that_is_Greenspace_2005": 36.4,
        "Total_carbon_emissions_2013": 752.4,
        "Household_Waste_Recycling_Rate_2013_14": 46.3,
        "Number_of_cars_2011_Census": 70421,
        "Number_of_cars_per_household_2011_Census": 1.1,
        "PC_of_adults_who_cycle_at_least_once_per_month_2013_14": 21.1,
        "Average_Public_Transport_Accessibility_score_2014": 2.9,
        "Achievement_of_5_or_more_A*-_C_grades_at_GCSE_or_equivalent_including_English_and_Maths_2013_14": 68.6,
        "Rates_of_Children_Looked_After_2014": 33,
        "PC_of_pupils_whose_first_language_is_not_English_2014": 31.2,
        "PC_children_living_in_out-of-work_households_2014": 8.9,
        "Male_life_expectancy_2011-13": 81.3,
        "Female_life_expectancy_2011-13": 84.5,
        "Teenage_conception_rate_2013": 15.8,
        "Life_satisfaction_score_2011-14_out_of_10": 7.3,
        "Worthwhileness_score_2011-14_out_of_10": 7.7,
        "Happiness_score_2011-14_out_of_10": 7.4,
        "Anxiety_score_2011-14_out_of_10": 3.3,
        "Childhood_Obesity_Prevalance_PC_2013_14": 15.4,
        "People_aged_17_with_diabetes_PC": 4.9,
        "Mortality_rate_from_causes_considered_preventable": 147.8,
        "Political_control_in_council": "Cons",
        "Proportion_of_seats_won_by_Conservatives_in_2014_election": 58.3,
        "Proportion_of_seats_won_by_Labour_in_2014_election": 4.2,
        "Proportion_of_seats_won_by_Lib_Dems_in_2014_election": 37.5,
        "Turnout_at_2014_local_elections": 43.1
    },
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
            [
                [
                    [-0.330679021773025, 51.32901101084708],
                    [-0.330594407566612, 51.329088040122315],
                    [-0.330506076681679, 51.329148829750466],
                    [-0.33040917032063, 51.32920859781458],
                    [-0.330292080679783, 51.329270775839156],
                    [-0.329913420803488, 51.32946051567418],
                    [-0.329633553448498, 51.3296570587963],
                    [-0.329428743663152, 51.32984208225338],
                    [-0.329320874515433, 51.32996643949609],
                    [-0.329214439452638, 51.330090817097144],
                    [-0.329116282383711, 51.330263872520085],
                    [-0.329112914578128, 51.330396015110445],
                    [-0.329171126538274, 51.33072957024903],
                    [-0.329191261142089, 51.330965462528575],
                    [-0.329302869370564, 51.33152819023752],
                    [-0.329362730358861, 51.33161896966427],
                    [-0.329554511157394, 51.33187169808366],
                    [-0.32957894743785, 51.332186786094184],
                    [-0.329594169463172, 51.33228142503257],
                    [-0.329689958909293, 51.33248961989663],
                    [-0.329719262840317, 51.332670788169274],
                    [-0.329718670292571, 51.3327661008151],
                    [-0.329713610148521, 51.33286584602793],
                    [-0.329643333783058, 51.33349612124335],
                    [-0.329628931512944, 51.33385292028404],
                    [-0.32955045530859, 51.33415574945231],
                    [-0.329484343656736, 51.334355340533264],
                    [-0.329546392185782, 51.33446503536734],
                    [-0.32943190788577, 51.334850082388876],
                    [-0.329410693783327, 51.33511775790784],
                    [-0.329359189787204, 51.33531036319948],
                    [-0.329208182584011, 51.33563373967703],
                    [-0.329197301934782, 51.335933036465214],
                    [-0.3292123737182, 51.33630824086913],
                    [-0.329192093632385, 51.33670812012244],
                    [-0.329041495932886, 51.33757285371855],
                    [-0.328900403109473, 51.33817603945666],
                    [-0.328869185702877, 51.33867917682473],
                    [-0.328882267763104, 51.33903007285105],
                    [-0.328865474006543, 51.33941291579552],
                    [-0.328870743501784, 51.339583849376474],
                    [-0.328876209135269, 51.33974939022238],
                    [-0.328959278855396, 51.34011027719941],
                    [-0.32897922859697, 51.34023286043202],
                    [-0.328909131031345, 51.34077860720778],
                    [-0.32878050967231, 51.34107802245298],
                    [-0.328542139079097, 51.34174996140999],
                    [-0.328525372819542, 51.34177669979828],
                    [-0.328346097965183, 51.34204751484793],
                    [-0.328201229588486, 51.34231971993927],
                    [-0.328010139783513, 51.3429545638654],
                    [-0.327944151115631, 51.343347495330974],
                    [-0.327906765100805, 51.34374353279829],
                    [-0.327796114780899, 51.34390381902136],
                    [-0.327480771031571, 51.34400722741141],
                    [-0.328177350406429, 51.34404145110161],
                    [-0.328483504737766, 51.344545805494214],
                    [-0.328666777138526, 51.344915316212365],
                    [-0.32889842247652, 51.34529720700365],
                    [-0.329205727742453, 51.345888803363664],
                    [-0.329345801048439, 51.34614349149164],
                    [-0.329970752009771, 51.34728006692206],
                    [-0.330327238877941, 51.34790203652441],
                    [-0.330533821707245, 51.3484211522831],
                    [-0.330367226671742, 51.348579744260604],
                    [-0.330215568888878, 51.34876192950989],
                    [-0.33010259433868, 51.34898603165988],
                    [-0.329936935927235, 51.349237259467515],
                    [-0.329751960800545, 51.34954576382794],
                    [-0.329563227290772, 51.34991806127805],
                    [-0.329472923004122, 51.350071445078235],
                    [-0.329311170759688, 51.35033351873695],
                    [-0.329035951591489, 51.350714473637105],
                    [-0.328765226477058, 51.351129663623325],
                    [-0.328353064876424, 51.351721786146854],
                    [-0.328046476146282, 51.35213556344137],
                    [-0.327946973892058, 51.352186299935575],
                    [-0.327759713433126, 51.35228074629348],
                    [-0.327275573431615, 51.352522028103955],
                    [-0.326841759688993, 51.352800896109116],
                    [-0.326479944940916, 51.353034929194735],
                    [-0.32576608192508, 51.35335295635771],
                    [-0.324837670800118, 51.353725463631605],
                    [-0.324453895670668, 51.35389173249815],
                    [-0.324158745463219, 51.35410872689187],
                    [-0.323752687942134, 51.354413159704905],
                    [-0.323535036520884, 51.35467082857294],
                    [-0.323184811812359, 51.355176591871896],
                    [-0.32286192079005, 51.35552357765017],
                    [-0.322576060678597, 51.355839618929],
                    [-0.322259713883874, 51.35620378257071],
                    [-0.321952974065641, 51.35661934039564],
                    [-0.321529222971824, 51.35721037383058],
                    [-0.321217561495061, 51.35768161289952],
                    [-0.320948159097397, 51.358096803885],
                    [-0.32074485191113, 51.35839334104852],
                    [-0.320259404554301, 51.35909948913235],
                    [-0.320079309555563, 51.3594287310578],
                    [-0.320178298703104, 51.35954975110903],
                    [-0.320165137159728, 51.35959542427937],
                    [-0.319956965481558, 51.359632006261116],
                    [-0.319792373194367, 51.35992999621074],
                    [-0.319546591138533, 51.360484007640004],
                    [-0.319408576659198, 51.360919064677674],
                    [-0.319187471491333, 51.36162270524168],
                    [-0.319060204553952, 51.362313303737686],
                    [-0.318285367823378, 51.36350628288284],
                    [-0.318304692373129, 51.36368461225171],
                    [-0.318410288479828, 51.364684298029104],
                    [-0.318436574395861, 51.36533933091015],
                    [-0.318446928565835, 51.36540962119001],
                    [-0.318522194031934, 51.365864823752055],
                    [-0.318523331127983, 51.366186772301226],
                    [-0.318419501486083, 51.366982019317454],
                    [-0.318293959925026, 51.36789925275619],
                    [-0.318118776705997, 51.36891648952965],
                    [-0.318016869219867, 51.36958047277866],
                    [-0.31792307432635, 51.3697877525771],
                    [-0.317915189904803, 51.369846090732615],
                    [-0.317905671937818, 51.36990980094361],
                    [-0.318029423835983, 51.370296457234964],
                    [-0.317949485943705, 51.37063522667972],
                    [-0.317747858873623, 51.371236628787635],
                    [-0.317505451116108, 51.371970534099816],
                    [-0.317485004351317, 51.37201880004826],
                    [-0.317397736511194, 51.37220459108471],
                    [-0.317284276827518, 51.37243856545665],
                    [-0.31710915300419, 51.37270852331906],
                    [-0.316949159320968, 51.37291844843032],
                    [-0.31669434018152, 51.37316747730857],
                    [-0.316426180103382, 51.373388437150716],
                    [-0.31622441906802, 51.37356179105688],
                    [-0.315991741423486, 51.373755383037754],
                    [-0.31566786597181, 51.37396744709197],
                    [-0.315222750497486, 51.374233520531305],
                    [-0.31478304313528, 51.37443042773603],
                    [-0.314341466820288, 51.374638996680865],
                    [-0.313886319633562, 51.374669316927424],
                    [-0.313377130221505, 51.374684469452525],
                    [-0.31296052429465, 51.374721635667065],
                    [-0.312674896375059, 51.37475259344767],
                    [-0.312510458790253, 51.374770008772465],
                    [-0.312155172284965, 51.37481974553786],
                    [-0.311542017783552, 51.37492691452625],
                    [-0.31108965287778, 51.374998629591076],
                    [-0.310483753449209, 51.375142766954085],
                    [-0.309642711208725, 51.375312285083936],
                    [-0.309096994839523, 51.3753826456651],
                    [-0.308695083240137, 51.37544968412024],
                    [-0.308457219737233, 51.37586080533736],
                    [-0.308342125073947, 51.376059676775654],
                    [-0.307993631387426, 51.376665236570894],
                    [-0.307964589950761, 51.37706318563219],
                    [-0.307842121582618, 51.37753981880234],
                    [-0.307892155265686, 51.378365154948014],
                    [-0.307388502353935, 51.3783848578608],
                    [-0.307520636068388, 51.379009947771415],
                    [-0.308301986382553, 51.38003288908768],
                    [-0.308325982763562, 51.38008359360354],
                    [-0.308517491830129, 51.38050181237386],
                    [-0.309276738904714, 51.3804489235838],
                    [-0.309318630671963, 51.38110598120351],
                    [-0.309245493241462, 51.3811408959084],
                    [-0.309282222270164, 51.38131408205096],
                    [-0.309293558960914, 51.38135740967078],
                    [-0.309317283472768, 51.38149353886625],
                    [-0.309300461368501, 51.381638075513614],
                    [-0.309302186977275, 51.38170824197235],
                    [-0.309330928191318, 51.38186422705928],
                    [-0.309498364267572, 51.38184416158862],
                    [-0.309558079241459, 51.38244662180848],
                    [-0.309654600932123, 51.382518155823895],
                    [-0.309751942685852, 51.38264545512327],
                    [-0.309842559115887, 51.38283830261783],
                    [-0.309886786708619, 51.38284703382125],
                    [-0.309911349075752, 51.382882458877866],
                    [-0.310347176087456, 51.38365220797289],
                    [-0.310288338333552, 51.38380603058168],
                    [-0.310307294453304, 51.38395468024451],
                    [-0.31041283493994, 51.38421068996167],
                    [-0.310442912142391, 51.38425248916564],
                    [-0.310540822314576, 51.38436450868583],
                    [-0.310654513111652, 51.38451632264458],
                    [-0.310566458115827, 51.38460497812067],
                    [-0.310580338529139, 51.38465733476053],
                    [-0.31063098206852, 51.3847650756968],
                    [-0.310665004889603, 51.38481682347486],
                    [-0.310729833733766, 51.38489059736828],
                    [-0.310950016955934, 51.38503855072464],
                    [-0.311127228786578, 51.38514361955623],
                    [-0.311787662820079, 51.385786208624666],
                    [-0.310868910629078, 51.38618752238585],
                    [-0.311409329096351, 51.386733062833684],
                    [-0.311458857237744, 51.38679312704089],
                    [-0.311499998462655, 51.386846775623816],
                    [-0.311363961797792, 51.386911359771595],
                    [-0.311693817532641, 51.38712563803517],
                    [-0.311822993635975, 51.38720843162237],
                    [-0.3117687866668, 51.3872364266419],
                    [-0.311735086833573, 51.387253926103],
                    [-0.31184550451329, 51.38737781488004],
                    [-0.31174002919236, 51.387432048754796],
                    [-0.312872428617744, 51.388490588702766],
                    [-0.313158544399437, 51.38880045260526],
                    [-0.313188957535943, 51.38883326338753],
                    [-0.313348146628585, 51.3887645141297],
                    [-0.315981046740714, 51.38763335801295],
                    [-0.316518343718128, 51.388266056660285],
                    [-0.316599042794471, 51.38845605832363],
                    [-0.316617277106647, 51.388585812140974],
                    [-0.316816040880735, 51.388574279405724],
                    [-0.316811782773372, 51.38861198670033],
                    [-0.316809818612619, 51.38862634647098],
                    [-0.316805824328563, 51.38865686356263],
                    [-0.316800426521383, 51.38868646124439],
                    [-0.316792155711511, 51.38871601765691],
                    [-0.31677817187624, 51.38874459275521],
                    [-0.316759878524943, 51.38877310594719],
                    [-0.316716538003206, 51.388818345067065],
                    [-0.316663273809364, 51.3888598446173],
                    [-0.316605798959337, 51.38889858590655],
                    [-0.31654548398803, 51.38893638711643],
                    [-0.316489643363005, 51.38896975633323],
                    [-0.316425150631468, 51.389003900464004],
                    [-0.31635785076633, 51.38903620572856],
                    [-0.315848095181505, 51.38925818893088],
                    [-0.315457631909052, 51.38948008640397],
                    [-0.314980758945598, 51.38974570252308],
                    [-0.314134077472051, 51.39021821967621],
                    [-0.314273949435976, 51.39036231326585],
                    [-0.314682226633619, 51.3907512659625],
                    [-0.314819872857466, 51.39091690883333],
                    [-0.314926939104345, 51.39105423508098],

如果用户想要返回所有功能的 HECTARES 以及随附的名称,我需要使用什么代码?

示例:用户输入 HECTARES,输出返回如下内容:Kingston Upon Thames:3726.117, Croydon:8649.441, etc.

示例 2:用户输入 GSS_CODE,输出返回如下信息:Kingston Upon Thames:"E09000021", Croydon:"E09000008", etc

到目前为止,我的代码如下所示:

app.get("/api/boroughs_field", function(req, res){

   // read input parameter from request
   var field = req.query['field'];

   // validate input, check if the parameter is valid or not
   if (field === undefined)
   {
      // return error as a JSON string
      var str = JSON.stringify( {"error":"Parameter 'field' is not defined."} );
      res.end( str );
   }

   var fldObj = null;

     // make a loop for fields???

   if (fldObj == null)
   {
      // field not found, return an error as a JSON string
      var str = JSON.stringify( {"error":"field '"+name+"' doesn't exist. Please check the spelling."} );
      res.end( str );
   }

   // If the program gets here, the borough has been found.
   var respJson = {};
   respJson["results"] = [ fldObj ];
   res.end( JSON.stringify( respJson ) );
   });
}

所以是的,我真的不知道如何编写循环代码以获得所需的结果

最佳答案

您可以遍历特征列表并使用 .hasOwnProperty('propName') 搜索对象的属性:

var feature;
var features = [];

for(var i = 0, total = jsonObj.features.length; i < total; i++) {
    feature = jsonObj.features[i];
    if (feature.hasOwnProperty(field)) {
        features.push({ name: feature.name, value: feature[field] });
    }
}

我建议你看看lodash以获得更多选项和性能。

关于javascript - 如何从所有功能中返回单个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852954/

相关文章:

javascript - 尝试运行 Selenium Webdriver (WebdriverJS) 的示例测试时出错

javascript - 使 HTML5 视频在指定时间停止

javascript - 坚持顶部菜单差距

javascript - 组合 2 个 JavaScript .scroll 函数

javascript - 如何安全地 XSS 编码来自 PHP 的不受信任的数据,通过 AJAX 通过 javascript 注入(inject)到 DOM 中?

Node.js HEAD http 请求

css - 将 gulp-clean-css 添加到现有的 gulpfile.js

javascript - 即时将文本框日期的格式更改为另一个文本框

javascript - jQuery/javascript 基础逻辑题

java - Jackson - 反序列化通用类变量