2015-04-01 72 views
0

嘗試使用下劃線更新下面的集合,但最後一次獲勝(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

請告訴我們什麼'availabilities'是。並確保使用三重平等'==='。 – 2015-04-01 23:14:18

回答

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>