简体   繁体   English

如何在javascript中读取geojson文件并获取最小或最大属性?

[英]How to read a geojson file in javascript and get min or max of property?

I'm new to js sorry if it's a stupid question.如果这是一个愚蠢的问题,我是 js 的新手,抱歉。 I guess there is two parts to my question.我想我的问题有两个部分。

What is the simplest way to read a geojson file with javascript ?使用 javascript 读取 geojson 文件的最简单方法是什么?

Right now I'm using :现在我正在使用:

var centrisData = $.getJSON( "Path to geojson");

My file looks something like that:我的文件看起来像这样:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "type": "Maison\u00a0",
        "utilisation": "null",
        "piecesBeta": "20.0",
        "price": 3995000.0,
        "prixPiece_beta": 199750.0,
      },
    },
    {
      "type": "Feature",
      "properties": {
        "type": "Condo\u00a0",
        "utilisation": "null",
        "piecesBeta": "6.0",
        "price": 448900.0,
        "prixPiece_beta": 74816.67
      }
    }

I want to be able to get the min and max of the property "price".我希望能够获得财产“价格”的最小值和最大值。 What would be the easiest way to do so ?这样做的最简单方法是什么?

Thanks in advance !提前致谢 !

You can loop over all of the features and keep and store the current minimum and maximum prices as variables.您可以遍历所有功能,并将当前的最低和最高价格作为变量保存和存储。

 const centrisData = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { "type": "Maison\ ", "utilisation": "null", "piecesBeta": "20.0", "price": 3995000.0, "prixPiece_beta": 199750.0, }, }, { "type": "Feature", "properties": { "type": "Condo\ ", "utilisation": "null", "piecesBeta": "6.0", "price": 448900.0, "prixPiece_beta": 74816.67 } } ] }; let min = Infinity, max = -min; for(const {properties: {price}} of centrisData.features){ min = Math.min(min, price); max = Math.max(max, price); } console.log("Min:", min); console.log("Max:", max);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM