javascript - 如何从另一个对象向 json 对象添加数据?

标签 javascript arrays json object

我有一个包含一些数据的数据对象,现在我想创建另一个对象 mapdata2,它与数据具有相同的结构。但我的代码没有工作,还显示了一些语法错误。 我创建了 mapdata2 对象并在其中创建了空要素数组。
它显示错误:

TypeError: i.features is undefined

<script>      
data = {"type": "FeatureCollection",
        "features": [
           {
                "type": "Feature", 
              "properties": 
              {
                "title": "ABC", "startDate": 1100, "endDate": 1200, "latitude": 60.814, "longitude": 11.845, "content": "content." 
              },
              "geometry": 
              {
                "type": "Point","coordinates": [ 60.814, 11.845, 1]
              } 
           },
           {
                "type": "Feature", 
              "properties": 
              {
                "title": "XYZ", "startDate": 1100, "endDate": 1200, "latitude": 40.814, "longitude": 15.845, "content": "content." 
              },
              "geometry": 
              {
                "type": "Point","coordinates": [ 40.814, 15.845, 1]
              } 
           },

         ]
     }
               mapdata2 = {
                  "type": "FeatureCollection",
                  "features" : []
                };

                for(i in data){
                    console.log(i);
                          mapdata2.features.push({
                            type:"Feature",
                            properties : { title: i.features.properties.title, startDate: i.features.properties.startDate, endDate: i.features.properties.endDate latitude: i.features.properties.latitude, longitude: i.features.properties.longitude, content: i.features.properties.content },
                            geometry : { type: "Point", coordinates: i.features.geometry.coordinates }
                        })
                  }
                  console.log(mapdata2);

 </script>

最佳答案

发生这种情况是因为您正在尝试为 data 中的每个 i 访问 features,但第一个 i"type" 并且其中没有 features

所以我修改了您的代码,以便您只迭代“功能”,并且对于每个功能,您都按照自己的方式进行操作,现在它可以正常工作了。

data = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "title": "ABC",
        "startDate": 1100,
        "endDate": 1200,
        "latitude": 60.814,
        "longitude": 11.845,
        "content": "content."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [60.814, 11.845, 1]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "XYZ",
        "startDate": 1100,
        "endDate": 1200,
        "latitude": 40.814,
        "longitude": 15.845,
        "content": "content."
      },
      "geometry": {
        "type": "Point",
        "coordinates": [40.814, 15.845, 1]
      }
    },

  ]
}
mapdata2 = {
  "type": "FeatureCollection",
  "features": []
};

data.features.forEach((feature) => {
  mapdata2.features.push({
    type: "Feature",
    properties: {
      title: feature.properties.title,
      startDate: feature.properties.startDate,
      endDate: feature.properties.endDate,
      id: feature.properties.id,
      latitude: feature.properties.latitude,
      longitude: feature.properties.longitude,
      content: feature.properties.content
    },
    geometry: {
      type: "Point",
      coordinates: feature.geometry.coordinates
    }
  })

});
console.log(mapdata2);

关于javascript - 如何从另一个对象向 json 对象添加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57771659/

相关文章:

java - 如何创建对象的 JSONObject?安卓

javascript - 表单验证检查后不显示模态框

javascript - 根据窗口大小加载jquery

java - jackson @JsonIgnore 属性为 null

json - 从oracle数据库中的json blob打印值

java - 如何从原始数组创建一个新数组,其中所有差异之和最大?

javascript - 让 setTimeout 工作

javascript - Bootstrap Accordion + x-editable

php - 如何从数组中删除重复的项

php - Ajax 访问 PHP 以 JSON 形式返回的多维数组