var Marker = function () {
this.Width = 12;
this.Height = 20;
this.XPos = 0;
this.YPos = 0;
this.name = ""
this.status = "e"
this.cstep = 0
this.id = 0
}
假設有五個不同的marker在Markers裡
var marker = new Marker();
marker.name = 'room'
marker.id = 2
marker.XPos = 50
marker.YPos = 100
Markers.push(marker);";
我嘗試像python的方法把Markers裡的marker以單獨分離出來
再對array裡的值做取代
for (var i = 0; i < Markers.length; i++) {
Markers[i].cstep += 1
}
結果每個marker裡的cstep 都沒有+1
請問該如何正確更新值?
var Maker = [
{
Width : 12,
Height : 20,
XPos : 0,
YPos : 0,
name : "room",
status : "e",
cstep : 0,
id : 0,
},
{
Width : 12,
Height : 20,
XPos : 0,
YPos : 0,
name : "Test",
status : "e",
cstep : 0,
id : 0,
},
{
Width : 12,
Height : 20,
XPos : 0,
YPos : 0,
name : "Jacky",
status : "e",
cstep : 0,
id : 0,
},
{
Width : 12,
Height : 20,
XPos : 0,
YPos : 0,
name : "Ken",
status : "e",
cstep : 0,
id : 0,
},
]
var c = 0;
for (var i = 0; i < Maker.length; i++) {
c+=1;
Maker[i].cstep = c;
console.log(Maker[i].cstep);
}
Markers.forEach( (e) => {
e.cstep += 1;
console.log(e.cstep);
})
//1, 1, 1, 1 ...
看起來是正常的,沒有甚麼問題,不曉得你問題出在哪?