下划分每个更新集合

时间:2015-04-01 23:04:54

标签: javascript underscore.js

尝试使用下划线更新以下集合,但最后一次胜利(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;
      }
   });
});

1 个答案:

答案 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>