【问题标题】:How to create JavaScript object and fill data dynamically? [closed]如何创建 JavaScript 对象并动态填充数据? [关闭]
【发布时间】:2019-08-26 14:15:21
【问题描述】:

我想用键值对动态创建一个数据对象。在这个对象中,还有一个名为 features 的数组,并且在这个名为 geometry 的数组元素中还有一个数组坐标。

在特征数组中,标题和内容等属性将动态添加,坐标数组的值也会添加。

如何在坐标数组中添加数据:“几何”:{“坐标”:[]}..?

data = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "title": "Day 1",
        "content": "This is where some people moved to."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -73.7949,
          40.7282,
          1
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "The Next Day",
        "content": "This is where some people grooved to."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.3838,
          40.9148,
          1
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "Amazing Event",
        "content": "This is where they went to have fun."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          4.899431,
          52.379189,
          1
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "1776",
        "content": "This where they went when the revolution had begun."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -71.3489484,
          42.4603719,
          1
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "1776",
        "content": "This where they went when the revolution had begun."

      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -71.2272,
          42.4473,
          1
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "1984",
        "content": "So they all came here...and disappeared without a trace!"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -0.118092,
          51.509865,
          1
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "12/22/63",
        "content": "Now, this can be quite the scary place."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -70.2553259,
          43.661471,
          1
        ]
      }
    },
  ]
}

var data = {

    features: []
    };
    for (piece in pieces){

     data.features.push({
            type: "Feature",
            properties: {title: '{piece.title}' , content: '{piece.content}' },
            geometry: {type: "Point"},

   });

  }

【问题讨论】:

标签: javascript arrays json object


【解决方案1】:

您已经尝试过什么,您的问题出在哪里? 你可以这样做:

var data = {
    features: []
};

data.features.push({
    type: "Feature"
});

【讨论】:

  • @MuhammadHassan 请不要在 cmets 中发布代码。它应该在你的问题中。 stackoverflow.com/help/how-to-ask
  • @psren 正是我正在这样做。现在我在我的问题中添加了一些代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 2019-02-17
  • 2018-10-31
  • 2013-11-10
  • 1970-01-01
相关资源
最近更新 更多