我使用CoffeeScript和Underscore.js來橫切一個對象,並設置一個如果它的屬性存在。這裏是我的代碼:在Underscore.js中設置對象屬性
_.each parsedJSON.name, (val) =>
if _.has(val, 'animation')
console.log 'Number of frames = ', _.size(val.frames)
val.animation.frames = _.size(val.frames)
我的代碼正確測試的對象中「動畫」的存在,並正確記錄中的數值的「框架」屬性的大小。
但val.animation.frames屬性保持不變。
僅供參考我上運行此對象是低於,讚賞任何幫助:
{
"name": {
"cinema": {
"size": {
"w": 256,
"h": 200
},
"tile": {
"baseW": 0,
"baseH": 0,
"drawW": 0,
"drawH": 0
},
"frame": {
"x": 0,
"y": 0,
"w": 256,
"h": 200
}
},
"dirt": {
"size": {
"w": 128,
"h": 64
},
"tile": {
"baseW": 0,
"baseH": 0,
"drawW": 0,
"drawH": 0
},
"animaton": {
"frames": 0,
"speed": 0
},
"frames": {
"frame1": {
"x": 128,
"y": 310,
"w": 128,
"h": 64
},
"frame2": {
"x": 128,
"y": 310,
"w": 128,
"h": 64
},
"frame3": {
"x": 128,
"y": 310,
"w": 128,
"h": 64
},
"frame4": {
"x": 128,
"y": 310,
"w": 128,
"h": 64
}
}
},
"grass": {
"size": {
"w": 128,
"h": 64
},
"tile": {
"baseW": 0,
"baseH": 0,
"drawW": 0,
"drawH": 0
},
"animaton": {
"frames": 0,
"speed": 0
},
"frames": {
"frame1": {
"x": 0,
"y": 310,
"w": 128,
"h": 64
},
"frame2": {
"x": 0,
"y": 310,
"w": 128,
"h": 64
}
}
},
"icecream": {
"size": {
"w": 128,
"h": 110
},
"tile": {
"baseW": 0,
"baseH": 0,
"drawW": 0,
"drawH": 0
},
"frame": {
"x": 128,
"y": 200,
"w": 128,
"h": 110
}
},
"tree": {
"size": {
"w": 128,
"h": 110
},
"tile": {
"baseW": 0,
"baseH": 0,
"drawW": 0,
"drawH": 0
},
"frame": {
"x": 0,
"y": 200,
"w": 128,
"h": 110
}
}
}
}
你怎麼看,它是不變的? – Bergi 2013-02-16 17:04:59