javascript - Underscore extend an object -
i want extend object add attribute:
var days = [{day:'monday'},{day:'tuesday'}]; _.extend(days,{day:'monday'},{name:'peter'}); // days not extended console.log(days); // want have result // [{day:'monday',name:'peter'},{day:'tuesday'}]
the goal able update or insert depending of criteria, want find 'monday' , extend, 'tuesday' , extend , on ... if find 'monday' again want remove name attribute , replace new criteria
============
update
this solution t.j crowder, update object if search new criteria same day :
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);
this output (here search 'monday' , insert peter, same ronald, , search 'monday' again , update name sandy) : code update insert new day 'monday' @ last of object
///// initial object ({ 'day':'monday' },{ 'day':'tuesday' }) ///// search 'monday' , add {name : 'peter'} ({ 'name':'peter', 'day':'monday' },{ 'day':'tuesday' },{ 'day':'monday' }) //// search 'monday' again , update {name : 'sandy'} ({ 'name':'sandy', 'day':'monday' },{ 'day':'tuesday' },{ 'day':'monday' },{ 'day':'monday' })
so question why extends {day:'monday'} each update ?
thank you
it does add properties array, they're not being shown, , they're not being added want them because aren't using _.extend
in way would. you're extending array; want extend array's first entry. here's what want, note [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>
of course, assumes know index of day: 'monday'
0
. if don't, you'll need find day first, perhaps using _.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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
in edit question, said code above keeps adding {day: 'monday'}
array. doesn't. let's put in function , repeatedly call function:
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 2 updates:"); console.log(days); extendday('monday', {name: 'peter'}); extendday('tuesday', {name: 'joseph'}); console.log("after second 2 updates:"); console.log(days);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
but why weren't seeing code: when console.log
array, many consoles make assumption you've used array in "normal" way , not added non-array-entry properties it. code adds non-array-entry properties array.
var days = [{day:'monday'},{day:'tuesday'}]; _.extend(days,{day:'monday'},{name:'peter'}); console.log(days); console.log(days.day); console.log(days.name);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Comments
Post a Comment