javascript - Smart way to add elements to all objects in array? js/jquery/knockout -
for example have array this:
var t = ko.observablearray([ {"foodname":"rice","foodoften":"2"}, {"foodname":"apple","foodoften":"3"} ]);
how can add static item of them below in smart way?
[ {"student":"1","foodname":"rice","foodoften":"2"}, {"student":"1","foodname":"apple","foodoften":"3"} ]
i know can loop t().length
add t()[n].student="1"
wondering there smart way knockout?
every variation on answering question going form of 'iterate on array , add value each object'. few:
t.foreach(function(entry) { entry.student = 1; }); var newt = t.map(function(entry) { entry.student = 1; return entry; }); (var = 0; i++; i< t.length) { t[i].student = 1; }
incidentally, @ least in chrome, jsperf says first efficient of three.
Comments
Post a Comment