简体   繁体   English

遍历Node.js中的多维数组

[英]Loop through multidimensional array in Node.js

Hey I got this object inside polygons.js: 嘿,我在polygons.js中得到了这个对象:

var polygons = [
  {
    "_id" : "12345",
    "geometry" : {
       "coordinates" : [[
           [9.123553, 48.71568],
           [ 9.119548, 48.71526 ]
       ]]
    }
  },
  {
    "_id" : "67890",
    "geometry" : {
       "coordinates" : [[
           [ 9.090445, 48.715736 ],
           [ 9.089583, 48.715687 ]
       ]]
    }
  }
]

I want to loop through this array in order to get a result like this: 我想遍历此数组以获得以下结果

[
  { 
    "_id" : "12345",
    "coordinates" : [[
      [9.123553, 48.71568],
      [ 9.119548, 48.71526 ]  
    ]]
  },
  { 
    "_id" : "67890",
    "coordinates" : [[
      [ 9.090445, 48.715736 ],
      [ 9.089583, 48.715687 ]  
    ]]
  }
]

Does anyone have an idea how to solve it? 有人知道如何解决吗? Thank you very much in regard! 非常感谢您!

you can map through the array and make the changes you need 您可以映射整个数组并进行所需的更改

formatted_polygons = polygons.map(function(polygon){
    return {
        coordinates : polygon.geometry.coordinates,
        _id : polygon._id
    }
});

Seems like you're just trying to get rid of the "geometry" prop and get the "coordinates" directly? 似乎您只是在尝试摆脱“几何”道具而直接获得“坐标”?

polygons.map(polygon => ({ _id: polygon._id, coordinates: polygon.geometry.coordinates }))

Should achieve what you're looking for 应该达到您的期望

You can simply use Array.map() : 您可以简单地使用Array.map()

 var polygons = [ { "_id" : "12345", "geometry" : { "coordinates" : [[ [9.123553, 48.71568], [ 9.119548, 48.71526 ] ]] } }, { "_id" : "67890", "geometry" : { "coordinates" : [[ [ 9.090445, 48.715736 ], [ 9.089583, 48.715687 ] ]] } } ]; let result = polygons.map(({_id,geometry})=>Object.assign({},{_id, coordinates :geometry.coordinates})); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM