大家好, 想請教一下關於JS 的問題
$.ajax({
type: 'get',
url: DEFAULT_API_PATH + "typhoon/typhoonList.php",
dataType: 'json'
}).done(function(res) {
var typhoonPositionArr = [];
var typhoonExtraDetailArr = [];
var typhoonAniTime = 15000;
$.each(res, function(key,arr) {
currentFileType = arr["file_type"];
tempArr = [];
if(!(currentFileType in typhoonPositionArr)) {
typhoonPositionArr[currentFileType] = [];
}
if(!(arr["initial_time"] in typhoonPositionArr[currentFileType])) {
typhoonPositionArr[currentFileType][arr["initial_time"]] = [];
}
if(!(currentFileType in typhoonExtraDetailArr)) {
typhoonExtraDetailArr[currentFileType] = [];
}
if(!(arr["initial_time"] in typhoonExtraDetailArr[currentFileType])) {
typhoonExtraDetailArr[currentFileType][arr["initial_time"]] = [];
}
tempArr.push(parseFloat(arr["latitude"]));
tempArr.push(parseFloat(arr["longitude"]));
tempArr.push(parseInt(arr["intensity"]));
// cannot apply the 4th value to array as it cannot pass the plugin function
//tempArr.push(parseInt(arr["initial_time"]));
typhoonPositionArr[currentFileType][arr["initial_time"]].push(tempArr);
typhoonExtraDetailArr[currentFileType][arr["initial_time"]].push(arr);
});
var typhobj = Object.assign({}, typhoonPositionArr);
var typhExtraobj = Object.assign({}, typhoonExtraDetailArr);
//console.log(typhobj);
console.log(typhobj);
//var polylineGroup = [];
$.each(typhobj, function(key, arr1) {
console.log(arr1);
$.each(arr1, function(keyinner, coorArr) {
console.log("fuk");
tempArr = [];
tempArr = coorArr;
//tempArr.push(coorArr);
//console.log(tempArr);
//tempColorArr = [];
var randomColor = "#" + Math.floor(Math.random()*16777215).toString(16);
var altThresholds = [10, 20, 30, 40, 50, 60, 70, 80];
points = tempArr;
//console.log(L.Motion.Polyline);
L.multiOptionsPolyline(tempArr, {
multiOptions: {
optionIdxFn: function (latLng) {
var i;
for (i = 0; i < altThresholds.length; ++i) {
if (latLng[2] <= altThresholds[i]) return i;
}
return altThresholds.length;
},
options: [
{color: '#e61818'}, {color: '#0040FF'}, {color: '#daff00'},
{color: '#00FFB0'}, {color: '#00E000'}, {color: '#80FF00'},
{color: '#FFFF00'}, {color: '#FFC000'}, {color: '#FF0000'}
]
},
weight: 5,
lineCap: 'butt',
opacity: 0.75,
smoothFactor: 1
}).addTo(map);
polyline = L.motion.polyline(tempArr, {color: "transparent"},{
auto: true,
duration: typhoonAniTime,
easing: L.Motion.Ease.easeInOutQuart
},{removeOnEnd: false}).addTo(map);
if( !(key in polylineGroup) ) {
polylineGroup[key] = polyline;
}
//console.log(polylineGroup);
//polylineGroup["H2123"].motionStart();
$.each(coorArr, function(key1,val){
//console.log(val);
var curArr = typhExtraobj[key][key1];
L.circleMarker(val, {
color: randomColor, fillColor: randomColor,radius: 3
}).bindTooltip("<strong>Coordinate:</strong> " + val[0]+","+val[1] + "<br><strong>File Type: </strong>" + curArr["file_type"] + "<br><strong>Intensity: </strong>" + curArr["intensity"]+ "<br><strong> Forecast time: </strong>" + curArr["forecast_time"]+ "<br><strong> Mean Sea Level Pressure: </strong>" + curArr["mean_sea_level_pressure"]).addTo(map);
});
});
不知道為何
typhoonPositionArr[currentFileType][arr["initial_time"]].push(tempArr);
這句push 之後, array length 是0 , 但裡面是有content的, 我是用了date 來做那個array key
[2021-09-02 00:00:00: Array(8)]
2021-09-02 00:00:00: (8) [Array(3), Array(3), Array(3), Array(3), Array(3), Array(3), Array(3), Array(3)]
length: 0
[[Prototype]]: Array(0)
首先,你要先了解一件事。
object 是沒有 length 值的。只有ARRAY有。
一但被轉成 object ,就只能用特別的方式來取得長度。