[
{
wFrom: {
fLatitude: "114.057868",
fLongitude: "22.543099",
fName: "深圳市"
},
wTo: {
tLatitude: "37.6173",
tLongitude: "55.755826",
tName: "Moscow"
},
wValue: "31",
wkey: "深圳市",
wproperties: "bag"
},
{
wFrom: {
fLatitude: "116.681972",
fLongitude: "23.354091",
fName: "汕頭市"
},
wTo: {
tLatitude: "37.6173",
tLongitude: "55.755826",
tName: "Moscow"
},
wValue: "24",
wkey: "汕頭市",
wproperties: "Baby Toys"
}
];
上面個數組怎麼遍歷構造出下面這種結構的數據:
var geodata = { "Moscow":[37.6173,55.755826],
"深圳市":[114.057868,22.543099],
"汕頭市":[116.681972,23.354091] };
求高手指點啊
<!DOCTYPE html>
<html>
<body>
<script>
var json=[
{
wFrom: {
fLatitude: "114.057868",
fLongitude: "22.543099",
fName: "深圳市"
},
wTo: {
tLatitude: "37.6173",
tLongitude: "55.755826",
tName: "Moscow"
},
wValue: "31",
wkey: "深圳市",
wproperties: "bag"
},
{
wFrom: {
fLatitude: "116.681972",
fLongitude: "23.354091",
fName: "汕頭市"
},
wTo: {
tLatitude: "37.6173",
tLongitude: "55.755826",
tName: "Moscow"
},
wValue: "24",
wkey: "汕頭市",
wproperties: "Baby Toys"
}
];
function get(){
var obj;
var wFrom;
var wTO;
var geodata ={};
for(var i=0;i<json.length;i++){
obj = json[i];
wFrom = obj['wFrom'];
wTO = obj['wTo'];
geodata[obj.wkey]=[wFrom.fLatitude,wFrom.fLongitude];
geodata[wTO.tName]=[wTO.tLatitude,wTO.tLongitude];
}
return geodata;
}
var geodata = get();
alert(geodata );
</script>
</body>
</html>