js 数据格式转换

var propertie = [
    {
        properties:{
            cp:[123.23,345],
            name: "吉林市"
        }
    },
    {
        properties:{
            cp:[123.23,345],
            name: "长春市"
        }
    }
]

要这样的数据格式

 var geoCoordMap = {
    '长春市': [122, 23],
    '吉林市': [126.661669, 45.742347],
    
}
阅读 1.7k
3 个回答
[
    {
        properties:{
            cp:[123.23,345],
            name: "吉林市"
        }
    },
    {
        properties:{
            cp:[123.23,345],
            name: "长春市"
        }
    }
].reduce((res, {properties: {cp, name}}) => (res[name] = cp, res), {})
function transform(data){
     const res = new Map()
     data.forEach(({properties:{name:key,cp:values} }) => {
         res.set(key, values)
    })
    return res
}
function transform(arr) {
    for (var i = arr.length, ret = {}; i--; ret[arr[i].properties.name] = arr[i].properties.cp);
    return ret;
}
console.dir(transform(propertie));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题