如何将另一个对象的数据添加到JSON对象中?

3

我有一个数据对象,其中包含一些数据,现在我想创建另一个名为 mapdata2 的对象,其结构与数据相同。但是我的代码没有运行成功,而且还显示了一些语法错误。

我已经创建了 mapdata2 对象并在其中创建了一个空的 features 数组。

它显示了一个错误:

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>
5个回答

4
那是因为您正在尝试访问data中的每个ifeatures,但第一个i"type",它里面没有features。 因此,我修改了您的代码,使您只对“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);


谢谢。@Omri Attyia。现在它可以工作了。我有一个问题。在我的数据中,几何图形中还有一个数组'coordinates'。如果我想要动态地向这个数组添加数据,那么我需要在顶部创建另一个数组吗? - Muhammad Hassan
1
@MuhammadHassan 如果你想向数组中添加数据,只需要使用 push。例如:mapdata2.features[0].geometry.coordinates.push(777); - Omri Attiya

3

如果您只想将 JSON 列表复制到 mapdata2 中,那么根本不需要进行映射或循环。

mapdata2.features = data.features
console.log(mapdata2);

这将实现你想要的目标,而无需循环。

    
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" : []
};

mapdata2.features = data.features
console.log(mapdata2);


2

看起来您想复制这个对象。您可以按照以下步骤进行操作:

最初的回答:

Object.assign({}, obj)

var mapdata2 = JSON.parse(JSON.stringify(data));

顺便提一下,你遇到了错误"TypeError: i.features is undefined",因为i是表示对象键的字符串。它将取值"type"和"features"。
要循环遍历数组data.features中的项目,您应该执行以下操作:
for (var i=0;i<data.features.length;i++) {
  var item = data.features[i];
}

2

这里有一些错误:

  • for循环中的in关键字遍历的是键,而不是值,我理解你想复制数据的是值。
  • 你试图在未访问数组features的索引之前访问该数组的properties属性。

将您代码的最后部分更改为:

for (let i = 0; i < features.length; i++) {
    layer.addData(features[i].properties);
}

"Original Answer"可以翻译成"最初的回答"。

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

"最初的回答" 可以做到 :)
但是,如果你只是想复制数据,我建议:
mapdata2 = {...data}
console.log(mapdata2)

1
一种保持您样式的“快速修复”:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接