为了账号安全,请及时绑定邮箱和手机立即绑定

如何分隔数组并将其放入括号中

如何分隔数组并将其放入括号中

30秒到达战场 2023-03-18 16:53:54
我有一个数组(Geojson 文件),我想将它分成三个数组。有一些多边形坐标,我想为每个 id 放置连续的坐标,我想得到: arrGeo =   [     [-4.66478, 58.42441, 5127.4,-4.65982, 58.42082, 5074.7],    [-3.94815, 57.71632, 5000,-3.94812, 57.71576, 4374.1,-3.94216, 57.71541, 4283,-3.93717,      57.71583, 5001],    [-3.93224, 57.71476, 4048,-3.93261, 57.71456, 3800.4]   ]我尝试通过使用 for 循环来做到这一点,但我无法将它们分开,我的意思是我需要为每个 ID 创建单独的坐标数组。我的代码有什么问题?我应该怎么做才能解决它?这是我的代码:positions =            [         {            "type": "Feature",            "geometry": {                "type": "GeometryCollection",                "geometries": [                    {                        "type": "Polygon",                        "coordinates": [                            [                                [-4.66478, 58.42441, 5127.4],                                [-4.65982, 58.42082, 5074.7],                            ]                        ]                    },                ]            },            "id": "kml_1"        },        {            "type": "Feature",            "geometry": {                "type": "GeometryCollection",                "geometries": [                    {                        "type": "Polygon",                        "coordinates": [                            [                                [-3.94815, 57.71632, 5000],                                [-3.94812, 57.71576, 4374.1],                            ]                        ]                    },                    {                        "type": "Polygon",                        "coordinates": [                            [                                [-3.94216, 57.71541, 4283],                                [-3.93717, 57.71583, 5001],                            ]                        ]                    },                ]            },            "id": "kml_2"        },
查看完整描述

4 回答

?
MMTTMM

TA贡献1869条经验 获得超4个赞


let final = [];

for (var i = 0, len = positions.length; i < len; i++) {

  let a = [];

  for (var j = 0, jlen = positions[i]["geometry"]["geometries"].length; j < jlen; j++) {

    

    for (var k = 0, klen = positions[i]["geometry"]["geometries"][j]["coordinates"].length; k < klen; k++) {

    

      for (l = 0, llen = positions[i]["geometry"]["geometries"][j]["coordinates"][k].length; l < llen; l++) {

        a = a.concat(positions[i]["geometry"]["geometries"][j]["coordinates"][k][l]);

      }

    }

  }

  final.push(a);

}

console.log(final);


查看完整回答
反对 回复 2023-03-18
?
SMILET

TA贡献1796条经验 获得超4个赞

您可以使用 forEach 来实现,以获得更好的性能。


let positions = [

        {

            type: "Feature",

            geometry: {

                type: "GeometryCollection",

                geometries: [

                    {

                        type: "Polygon",

                        coordinates: [

                            [

                                [-4.66478, 58.42441, 5127.4],

                                [-4.65982, 58.42082, 5074.7],

                            ],

                        ],

                    },

                ],

            },

            id: "kml_1",

        },

        {

            type: "Feature",

            geometry: {

                type: "GeometryCollection",

                geometries: [

                    {

                        type: "Polygon",

                        coordinates: [

                            [

                                [-3.94815, 57.71632, 5000],

                                [-3.94812, 57.71576, 4374.1],

                            ],

                        ],

                    },

                    {

                        type: "Polygon",

                        coordinates: [

                            [

                                [-3.94216, 57.71541, 4283],

                                [-3.93717, 57.71583, 5001],

                            ],

                        ],

                    },

                ],

            },

            id: "kml_2",

        },


        {

            type: "Feature",

            geometry: {

                type: "GeometryCollection",

                geometries: [

                    {

                        type: "Polygon",

                        coordinates: [

                            [

                                [-3.93224, 57.71476, 4048],

                                [-3.93261, 57.71456, 3800.4],

                            ],

                        ],

                    },

                ],

            },

            id: "kml_3",

        },

    ];



    let newPolygons = []



    const flatPositions = (arr)=>{

        arr.forEach((pos)=>{

          let p = []

            pos?.geometry?.geometries.forEach((geometry)=>{

              geometry?.coordinates.forEach(coord=>{

                coord.forEach(coo=>{

                  p = p.concat(coo)

                })

            })

            })

          newPolygons.push(p)

        })


    }

    flatPositions(positions)


    console.log(newPolygons)


查看完整回答
反对 回复 2023-03-18
?
噜噜哒

TA贡献1784条经验 获得超7个赞

只需使用Array.prototype.reduce()
和Array.prototype.flat()
以及解构赋值

const arrGeo = positions.reduce((a,{geometry})=>

  {

  let item = []

  for (let geo of geometry.geometries)

    {

    let p = geo.coordinates.reduce((t,c)=>

      {

      t.push(...c.flat())

      return t

      },[])

      item.push(...p)

    }

  a.push(item)

  return a

  },[])

完整代码:

const positions = 

      [ { type: 'Feature'

        , geometry: 

          { type: 'GeometryCollection'

          , geometries: 

            [ { type: 'Polygon'

              , coordinates: 

                [ [ [ -4.66478, 58.42441, 5127.4] 

                  , [ -4.65982, 58.42082, 5074.7] 

          ] ] } ] } 

        , id: 'kml_1'

        } 

      , { type: 'Feature'

        , geometry: 

          { type: 'GeometryCollection'

          , geometries: 

            [ { type: 'Polygon'

              , coordinates: 

                [ [ [ -3.94815, 57.71632, 5000   ] 

                  , [ -3.94812, 57.71576, 4374.1 ] 

              ] ] } 

            , { type: 'Polygon'

              , coordinates: 

                [ [ [ -3.94216, 57.71541, 4283] 

                  , [ -3.93717, 57.71583, 5001] 

          ] ] } ] } 

        , id: 'kml_2'

        } 

      , { type: 'Feature'

        , geometry: 

          { type: 'GeometryCollection'

          , geometries: 

            [ { type: 'Polygon'

              , coordinates: 

                [ [ [ -3.93224, 57.71476, 4048] 

                  , [ -3.93261, 57.71456, 3800.4] 

          ] ] } ] } 

        , id: 'kml_3'

      } ] 


const arrGeo = positions.reduce((a,{geometry})=>

  {

  let item = []

  for( let geo of geometry.geometries )

    {

    let p = geo.coordinates.reduce((t,c)=>

      {

      t.push(...c.flat())

      return t

      },[])

      item.push(...p)

    }

  a.push(item)

  return a

  },[])


arrGeo.forEach(el=> console.log(JSON.stringify(el)))

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2023-03-18
?
慕莱坞森

TA贡献1810条经验 获得超4个赞

我不确定,但如果我理解正确,这应该是理想的结果。我希望我有所帮助。


positions =


    [

        {


            "type": "Feature",

            "geometry": {

                "type": "GeometryCollection",

                "geometries": [

                    {

                        "type": "Polygon",

                        "coordinates": [

                            [

                                [-4.66478, 58.42441, 5127.4],

                                [-4.65982, 58.42082, 5074.7],


                            ]

                        ]

                    },

                ]

            },

            "id": "kml_1"

        },

        {

            "type": "Feature",

            "geometry": {

                "type": "GeometryCollection",

                "geometries": [

                    {

                        "type": "Polygon",

                        "coordinates": [

                            [

                                [-3.94815, 57.71632, 5000],

                                [-3.94812, 57.71576, 4374.1],


                            ]

                        ]

                    },

                    {

                        "type": "Polygon",

                        "coordinates": [

                            [

                                [-3.94216, 57.71541, 4283],

                                [-3.93717, 57.71583, 5001],


                            ]

                        ]

                    },


                ]

            },

            "id": "kml_2"

        },


        {

            "type": "Feature",

            "geometry": {

                "type": "GeometryCollection",

                "geometries": [

                    {

                        "type": "Polygon",

                        "coordinates": [

                            [

                                [-3.93224, 57.71476, 4048],

                                [-3.93261, 57.71456, 3800.4],


                            ]

                        ]

                    },

                ]

            },

            "id": "kml_3"

        },

    ];


var arrGeo = [];

var res = [];


for (var i = 0; i < positions.length; i++) {

    var x = positions[i].geometry.geometries;

    var f = [];

    for (var ii = 0; ii < x.length; ii++) {

        var z = x[ii].coordinates[0];

        var t = z.flat();

        f.push(t)

    }

    res.push(f);

    for (let ii = 0; ii < res.length; ii++) {

        res[ii].flat();

    }

}


for (var i = 0; i < res.length; i++) {

    arrGeo.push(res[i].flat());

}


console.log(arrGeo);


查看完整回答
反对 回复 2023-03-18
  • 4 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信