I was initially trying to do this:
var array = ['A','B','C'];
array = _.each(array, function(item) { return item + '+' });
to get this:
['A+','B+','C+'];
but _.each does not seem to return a function.
I eventually settled on this:
var array = ['A','B','C'];
newArray = [];
_.each(array, function(item) {
newArray.push(item + '+');
});
but it seems pretty clunky by parison.
Am I using _.each incorrectly, or is there another function I should be using?
I was initially trying to do this:
var array = ['A','B','C'];
array = _.each(array, function(item) { return item + '+' });
to get this:
['A+','B+','C+'];
but _.each does not seem to return a function.
I eventually settled on this:
var array = ['A','B','C'];
newArray = [];
_.each(array, function(item) {
newArray.push(item + '+');
});
but it seems pretty clunky by parison.
Am I using _.each incorrectly, or is there another function I should be using?
Share asked Oct 3, 2013 at 0:43 Klatch BaldarKlatch Baldar 598 bronze badges 1- 1 use _.map to return an array - _.each is just an iterator method – kinakuta Commented Oct 3, 2013 at 0:46
3 Answers
Reset to default 6is there another function I should be using?
Yes. each
never returns an array, it just iterates. Use map
to produce an array of callback results.
var array = _.map(['A','B','C'], function(item) { return item + '+' });
Making my ment an answer instead - use _.map to return an array. The _.each method will simply iterate through your collection.
var newArray = _.map(array, function(item) {
return item + '+';
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744931219a4601750.html
评论列表(0条)