2013-02-16 158 views
0

我使用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 
      } 
     } 
    } 
} 
+0

你怎麼看,它是不變的? – Bergi 2013-02-16 17:04:59

回答

1

這只是一個錯字:animatonanimation

+0

我更正了錯字,但動畫中的框架屬性仍未更改。 – 2013-02-16 16:23:49

+0

@JamieFearon,它仍然不適合你或一切都好嗎?我的控制檯:https://dl.dropbox.com/u/15832761/temp/undrscr.png – 2013-02-16 17:37:43

+0

現在正在工作,謝謝。 – 2013-02-16 18:39:23