我想扩展一个对象以添加另一个属性:
var days = [{day:'monday'},{day:'tuesday'}];
_.extend(days,{day:'monday'},{name:'Peter'});
// DAYS NOT EXTENDED
console.log(days);
// I WANT TO HAVE AS RESULT
// [{day:'monday',name:'Peter'},{day:'tuesday'}]
目标是能够根据标准更新或插入,我想找到'星期一'并延长,'星期二'和延伸等等...... 如果我再次找到'星期一',我想删除名称属性并用新标准替换它
============
这是来自T.J Crowder的解决方案,如果我使用新条件但是在同一天搜索,它会更新对象:
var days = [{day:'monday'},{day:'tuesday'}];
var day = _.find(days, function(day) {
return day.day == 'monday';
});
if (day) {
_.extend(day,{day:'monday'},{name:'Peter'});
}
console.log(days);
此输出(此处我搜索'星期一'并插入彼得,我对罗纳德也这样做,我再次搜索'星期一'并用桑迪更新名称): 代码更新,但也在对象的最后一天插入新的'星期一'
///// THE initial object
({
'day':'Monday'
},{
'day':'Tuesday'
})
///// search 'Monday' and add {name : 'Peter'}
({
'name':'Peter',
'day':'Monday'
},{
'day':'Tuesday'
},{
'day':'Monday'
})
//// search 'Monday' again and update with {name : 'Sandy'}
({
'name':'Sandy',
'day':'Monday'
},{
'day':'Tuesday'
},{
'day':'Monday'
},{
'day':'Monday'
})
所以我的问题是为什么它会延长{day:'Monday'}每次更新?
谢谢
答案 0 :(得分:2)
它 将这些属性添加到数组中,它们只是没有被显示,并且它们没有被添加到您希望它们的位置,因为您不是使用_.extend
的方式。您正在扩展数组;你想扩展数组的第一个条目。这就是你想做什么,请注意[0]
:
var days = [{day:'monday'},{day:'tuesday'}];
_.extend(days[0],{day:'monday'},{name:'Peter'});
// Note -----^^^
console.log(days);

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
当然,假设您知道day: 'monday'
的索引是0
。如果你不这样做,你需要先找到这一天,也许是使用_.find
:
var days = [{day:'monday'},{day:'tuesday'}];
var day = _.find(days, function(day) {
return day.day == 'monday';
});
if (day) {
_.extend(day,{day:'monday'},{name:'Peter'});
}
console.log(days);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
在您对问题的编辑中,您说上面的代码一直在向数组添加{day: 'monday'}
。它没有。让我们把它放在一个函数中,然后重复调用该函数:
function extendDay(dayName, properties) {
var day = _.find(days, function(day) {
return day.day == dayName;
});
if (day) {
_.extend(day, properties);
}
}
var days = [{day:'monday'},{day:'tuesday'}];
extendDay('monday', {name: 'James'});
extendDay('tuesday', {name: 'Sandy'});
console.log("after first two updates:");
console.log(days);
extendDay('monday', {name: 'Peter'});
extendDay('tuesday', {name: 'Joseph'});
console.log("after second two updates:");
console.log(days);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
但至于为什么你的代码没有看到任何内容:当你console.log
一个数组时,许多控制台会假设你已经在&#34; normal&#中使用了数组#34;方式,而不是添加非数组输入属性。您的代码将非数组条目属性添加到数组中。
var days = [{day:'monday'},{day:'tuesday'}];
_.extend(days,{day:'monday'},{name:'Peter'});
console.log(days);
console.log(days.day);
console.log(days.name);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
答案 1 :(得分:1)
您可以循环更改day
为Monday
var days = [{day:'monday'},{day:'tuesday'}];
var dayName = "monday"
var propExtend = _.extend({day:dayName},{name:'Peter'});
for(var i = 0; i< days.length; i++){
if(days[i].day == dayName){
days[i] = propExtend;
}
}
console.log(days)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
答案 2 :(得分:1)
var days = [{day:'monday'},{day:'tuesday'}];
var obj =_.where(days, {day: "Monday"});
_.extend(obj[0], {name:'Peter'});
for(int i=1; i<obj.length;i++){
_.extend(obj[i], {newCriteria:'your value'});
}
这将改变它,因为它具有相同的对象引用。
虽然你的问题对于如何处理事件并不太具体