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

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

我是 js 的新手,如果这是一个愚蠢的问题,我深表歉意。我想我的问题有两个部分。

使用 javascript 读取 geojson 文件的最简单方法是什么?

现在我正在使用:

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

我的文件看起来像这样:

{
  "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
      }
    }

我希望能够获得 属性“价格”的最小值和最大值。最简单的方法是什么?

提前致谢!

您可以遍历所有功能并将当前最低和最高价格保存并存储为变量。

const centrisData = {
  "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
      }
    }
  ]
};
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);