方法介绍
flatMap()方法是数组的一个高阶函数,它结合了
map()和flat()的功能,先对数组每个元素执行映射操作,再将结果扁平化一层。
语法
const newArray = arr.flatMap(callback(currentValue[, index[, array]])[, thisArg])
- callback:处理每个元素的函数,返回新数组元素
- thisArg(可选):执行回调时的this值
例子
使用flatMap方法只获取data数据中的经纬度信息coordinates
const data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
117.086382,
36.7084
],
[
117.14885,
36.718358
],
[
117.142672,
36.694663
],
[
117.086382,
36.7084
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
117.06167,
36.67921
],
[
117.170474,
36.657575
],
[
117.085353,
36.650363
],
[
117.06167,
36.67921
]
]
]
}
}
]
}
if (data && data.features) {
this.resultData = data.features.flatMap((feature) => {
if (feature.geometry && feature.geometry.coordinates) {
return feature.geometry.coordinates;
}
return [];
});
}
1041

被折叠的 条评论
为什么被折叠?



