const data = [
{
"id": 1,
"geom_xy": "POINT (0 0)",
"geom_z": "0"
},
{
"id": 2,
"geom_xy": "LINESTRING (1 1, 2 2, 3 3)",
"geom_z": "1,2,3"
},
{
"id": 3,
"geom_xy": "POLYGON ((4 4, 5 5, 6 6, 7 7, 4 4))",
"geom_z": "4,5,6,7,4"
}
]
// 转换成
const result = [
{
id: 1,
geom: {
type: "Point",
coordinates: [
[0, 0, 0]
]
}
}
]
function transArr(data) {
return data.reduce((acc, curr) => {
let id = curr.id
let geom0 = curr.geom_xy.split('(')
let geom1 = geom0[geom0.length-1].split(')')[0].split(',')
let geomz = curr.geom_z.split(',')
let geom3D = geom1.reduce((acc1, curr1, index1) => {
return acc1.concat( [curr1.trim().split(' ').concat(geomz[index1]) ] )
}, [])
// console.log(geom3D)
let type = curr.geom_xy.split('(')[0].trim()
let obj = {
id: id,
geom: {
type: type,
coordinates: geom3D
}
}
acc.push(obj)
return acc
}, [])
}
console.log(transArr(data))
const data1 = [
{
key: 'name',
value: '123'
},
{
key: 'age',
value: '456'
},
{
key: 'from',
value: 'beijing'
}
]
function trans(arr) {
// let obj = {}
// let obj = new Map()
// let o = arr.reduce((acc, curr) => {
// return acc.set(curr.key, curr.value)
// }, obj)
// for(let k of arr) {
// obj[k.key] = k.value
// }
// return obj
return arr.reduce(function(acc, cur) {
// let k = cur.key
// console.log(k, cur.value)
acc[cur.key] = cur.value
return acc
},{})
}
console.log(trans(data1))
console