1
我刚开始使用Underscore.js,并希望得到一些关于如何执行以下操作的建议。我有以下的JSON已经被解析并存储在parsedJson变量:Underscore.js - 迭代对象和计算对象属性
{
"name": {
"cinema": {
"size": {
"w": 256,
"h": 200
},
"frame": {
"x": 0,
"y": 0,
"w": 256,
"h": 200
}
},
"dirt": {
"size": {
"w": 128,
"h": 64
},
"animaton": {
"frames": 0,
"speed": 0
},
"frames": {
"frame1": {
"x": 128,
},
"frame2": {
"x": 128,
},
"frame3": {
"x": 128,
},
"frame4": {
"x": 128,
}
}
},
"grass": {
"size": {
"w": 128,
"h": 64
},
"animaton": {
"frames": 0,
"speed": 0
},
"frames": {
"frame1": {
"x": 0,
},
"frame2": {
"x": 0,
}
}
},
"icecream": {
"size": {
"w": 128,
"h": 110
},
"frame": {
"x": 128,
}
},
"tree": {
"size": {
"w": 128,
"h": 110
},
"frame": {
"x": 0,
}
}
}
}
我想实现的是设置animation.frames值(如果存在)在帧财产的帧数。
所以dirt.animation.frames值应该被设置为4。
和grass.animation.frames值应该被设置为2
据我所知,计数n个对象中的属性数我可以使用_size(),但我应该如何遍历对象中的每个名称并设置animation.speed值(如果存在)。这个对象也是动态的,所以可以有任意数量的名字。
感谢您的帮助,答案将启动我如何使用underscore.js的正确方向。
注:我使用的CoffeeScript