js数组方法之flatMap

js数组方法之flatMap

方法介绍

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 [];
   });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值