亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何分隔數組并將其放入括號中

如何分隔數組并將其放入括號中

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 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號