尝试使用下划线更新以下集合,但最后一次胜利(READ_ONLY变为false)会覆盖所有以前的更新。知道怎么解决吗?
_.each(availabilities, function(availability, availIndex) {
_.each(availability.POINTS, function(point, availPointIndex) {
if(availIndex == 0) {
point.READ_ONLY = true;
} else {
point.READ_ONLY = false;
}
});
});
答案 0 :(得分:0)
这应该有效:
var availabilities = [
{
POINTS:[{READ_ONLY:true}, {READ_ONLY:true}, {READ_ONLY:true}]
},
{
POINTS:[{READ_ONLY:true}, {READ_ONLY:true}, {READ_ONLY:true}]
},
{
POINTS:[{READ_ONLY:true}, {READ_ONLY:true}, {READ_ONLY:true}]
}
]
_.map(availabilities, function(availability, availIndex) {
_.mapObject(availability.POINTS, function(point, availPointIndex) {
if(availIndex == 0) {
point.READ_ONLY = true;
} else {
point.READ_ONLY = false;
}
});
});
console.log(availabilities);
<script src="http://underscorejs.org/underscore-min.js"></script>
<pre>
[Object, Object, Object]
0: Object
POINTS: Array[3]
0: Object READ_ONLY: true
1: Object READ_ONLY: true
1: Object READ_ONLY: true
1: Object
POINTS: Array[3]
0: Object READ_ONLY: false
1: Object READ_ONLY: false
1: Object READ_ONLY: false
2: Object
POINTS: Array[3]
0: Object READ_ONLY: false
1: Object READ_ONLY: false
1: Object READ_ONLY: false
</pre>