Javascript 如何从所有功能返回单个字段?
我需要返回单个字段的所有特性,并且只返回单个字段,而不是整个数据集 我的json文件具有以下格式:Javascript 如何从所有功能返回单个字段?,javascript,json,node.js,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"
{"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%的内容)
比如说,如果用户想要返回所有功能的公顷数以及附带的名称,我需要使用什么代码
示例:用户输入公顷,输出返回类似于:泰晤士河上的金斯敦:3726.117,克罗伊登:8649.441,等等
示例2:用户输入GSS_代码,输出返回类似于:泰晤士河上的金斯敦:“E09000021”,克罗伊登:“E0900008”,等等
到目前为止,我的代码如下所示:
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 ) );
});
}
因此,是的,我真的不知道如何对循环进行编码以获得所需的结果,所以我认为您需要下面这样的内容。我省略了很多周围的细节,它们有很多细微差别,但这是对你问题的一般回答
var i, fLength, fieldValue, nameValue;
// HECTARES, GSS_CODE, etc
var requestedFieldName = ???;
// Not quite understanding your return object so you'll have to figure this out
var features = something.features;
// an array of what you were looking for
var res = [];
// Standard loop, instead of checking the length every loop though
// we set it as a constant to start with and check that constant.
for (i=0, fLength = features.length; i<fLength; i++) {
// Javascript objects are a hashmap by their nature,
// normally you can use dot notation myVar.myVal,
// but that only works when myVal is a constant string.
// If it is a variable, as in this case you can use [] instead.
fieldValue = features[i][requestedFieldName];
nameValue = features[i]["NAME"];
res.push({"field":fieldValue", "name":nameValue});
}
变量i、长度、字段值、名称值;
//公顷、GSS_代码等
var requestedFieldName=???;
//不太了解返回对象,所以必须弄清楚这一点
var features=something.features;
//一系列你要找的东西
var-res=[];
//标准循环,而不是检查每个循环的长度
//我们将其设置为一个常量,并检查该常量。
对于(i=0,fLength=features.length;i您可以循环浏览功能列表,并使用.hasOwnProperty('propName')
搜索对象的属性:
var特征;
var特征=[];
对于(var i=0,total=jsonObj.features.length;i
我建议您查看更多选项和性能。您能正确格式化JSON吗?(这将有助于理解您的问题)我在公顷处做了一个换行符,这应该可以帮助您通过json lint或类似的方式运行json。特别是对于您的问题,如果我理解正确,您只是在公顷为特定值时尝试访问name属性?在这种情况下,您将编写一个使用简单属性访问属性的循环成功。@blue否,我想返回所有自治区的所选字段(该名称极大地帮助用户查看哪个自治区具有给定的值)@Nonemoticoner res.end在代码的其他部分对我很好。如何在没有推送的情况下组合fieldValue和nameValue?我需要执行类似于var respJson={};respJson[“results”]=[fieldValue和name value];
这样我就可以通过执行res.end(json.stringify(respJson)),将输出转换为json;
数组可以通过json进行字符串化,您将得到类似[{field:E09000021,name:“泰晤士河上的金斯敦”},{field:X09000021,name:“金斯敦安大略”}]。推送本身包含对对象的转换,其中的{}正在动态生成对象。如果您想将其设置为{fieldName:fieldValue,“name”:nameValue},也可以。对于特定情况,您可以执行respJson[“results”]=res之类的操作。出于某种原因,我只使用`respJson[“results”]获得空结果=[fieldValue];`然后是res.end(JSON.stringify(respJson));。您建议的循环似乎找不到任何结果,无论我使用哪个输入。我无法使用push,因此无法验证您的代码是否有效。我需要使用var respJson={};respJson[“respJson”]=[fieldValue和name值]
;这样我就可以通过执行res.end(json.stringify(respJson)),将输出转换为json;
我已经修复了if块中的一个输入错误,在if块中,push()
方法被调用到字段变量(该变量不存在)。正确的是features.push()
。您想要的结果将出现在功能中。
var i, fLength, fieldValue, nameValue;
// HECTARES, GSS_CODE, etc
var requestedFieldName = ???;
// Not quite understanding your return object so you'll have to figure this out
var features = something.features;
// an array of what you were looking for
var res = [];
// Standard loop, instead of checking the length every loop though
// we set it as a constant to start with and check that constant.
for (i=0, fLength = features.length; i<fLength; i++) {
// Javascript objects are a hashmap by their nature,
// normally you can use dot notation myVar.myVal,
// but that only works when myVal is a constant string.
// If it is a variable, as in this case you can use [] instead.
fieldValue = features[i][requestedFieldName];
nameValue = features[i]["NAME"];
res.push({"field":fieldValue", "name":nameValue});
}
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] });
}
}