【问题标题】:How to add data in json object from another object?如何从另一个对象向 json 对象中添加数据?
【发布时间】:2019-09-03 12:21:18
【问题描述】:

我有一个包含一些数据的数据对象,现在我想创建另一个对象mapdata2,它与数据具有相同的结构。但是我的代码不起作用,并且还显示了一些语法错误。 我在其中创建了 mapdata2 对象和空特征数组。
它显示一个错误:

TypeError: i.features 未定义

<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>

【问题讨论】:

    标签: javascript arrays json object


    【解决方案1】:

    发生这种情况是因为您尝试访问 i 中的每个 i data,但第一个 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);

    【讨论】:

    • 谢谢。 @Omri Attiya。现在它的工作。我有个问题。在我的数据中,几何中还有一个数组“坐标”。如果我想在这个数组中动态添加数据,那么我需要在顶部再创建一个数组。 ?
    • @MuhammadHassan 如果要向数组添加数据,只需使用push。例如:mapdata2.features[0].geometry.coordinates.push(777);
    【解决方案2】:

    如果您只是想将 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);

    【讨论】:

      【解决方案3】:

      您似乎想复制该对象。您可以简单地执行以下操作。

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

      顺便说一句,您收到错误“TypeError: i.features is undefined”,因为 i 是表示对象键的字符串。它将采用值“类型”和“特征”。

      要遍历数组 data.features 中的项目,您应该这样做:

      for (var i=0;i<data.features.length;i++) {
        var item = data.features[i];
      }
      

      【讨论】:

        【解决方案4】:

        这里有一些错误:

        • in for bucle 中的关键字迭代的是键,而不是值,我知道这是你想要复制数据的方式。
        • 您正在尝试访问数组 features 的属性 properties,而不先访问该数组的索引。

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

        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)
        

        【讨论】:

          【解决方案5】:

          保持您的风格的“快速修复”:

          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 
                  }
              })
          }
          

          【讨论】:

            猜你喜欢
            • 2011-02-08
            • 1970-01-01
            • 1970-01-01
            • 2012-06-20
            • 1970-01-01
            • 2020-05-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多