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

问题描述

我有一个数组(Geojson文件),我想将其拆分为三个array。有一些多边形坐标,我想为每个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,5127.4],[-4.65982,]
                        ]
                    },]
            },"id": "kml_1"
        },{
            "type": "Feature","coordinates": [
                            [
                                [-3.94815,5000],[-3.94812,4374.1],{
                        "type": "polygon","coordinates": [
                            [
                                [-3.94216,4283],[-3.93717,"id": "kml_2"
        },"coordinates": [
                            [
                                [-3.93224,4048],[-3.93261,3800.4],"id": "kml_3"
        },];

    var customMesh = new BABYLON.Mesh("custom",scene);
    var customMesh2 = new BABYLON.Mesh("custom",scene);
    let arrGeo = [];
    let indices = [];



    for (let i = 0; i < positions.length; i++) {
        let fGeometry = positions[i].geometry.geometries;

        console.log("i",fGeometry);

        for (let j = 0; j < fGeometry.length; j++) {
            console.log("j",j);
            // console.log("p",fGeometry[1]);
            for (let k = 0; k < 1; k++) {
                for (let m = 0; m < 3; m++) {
                    for (let n = 0; n < 3; n++) {
                        let fGCoordinate = fGeometry[j].coordinates[k][m][n];
                        console.log("p",fGCoordinate);

                        arrGeo.push(fGCoordinate);
                    }
                }
            }
        }

    }

解决方法

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

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","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"
        },"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);

,

请在下面找到代码和演示链接

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

https://jsbin.com/hehurajoje/edit?js,console

,

只需使用Array.prototype.reduce()
Array.prototype.flat()
Destructuring assignment

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,[ -4.65982,5074.7] 
          ] ] } ] },id: 'kml_1'
        },{ type: 'Feature',coordinates: 
                [ [ [ -3.94815,5000   ],[ -3.94812,4374.1 ] 
              ] ] },{ type: 'Polygon',coordinates: 
                [ [ [ -3.94216,[ -3.93717,5001] 
          ] ] } ] },id: 'kml_2'
        },coordinates: 
                [ [ [ -3.93224,[ -3.93261,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,[])

arrGeo.forEach(el=> console.log(JSON.stringify(el)))
.as-console-wrapper { max-height: 100% !important; top: 0; }

,

您可以使用forEach做到这一点,以获得更好的性能。

let positions = [
        {
            type: "Feature",geometry: {
                type: "GeometryCollection",geometries: [
                    {
                        type: "Polygon",coordinates: [
                            [
                                [-4.66478,],},id: "kml_1",{
            type: "Feature",coordinates: [
                            [
                                [-3.94815,{
                        type: "Polygon",coordinates: [
                            [
                                [-3.94216,id: "kml_2",coordinates: [
                            [
                                [-3.93224,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)