javascript - UnderscoreLo-Dash _.each not returning array - Stack Overflow

I was initially trying to do this:var array = ['A','B','C'];array = _.ea

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
Add a ment  | 

3 Answers 3

Reset to default 6

is 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信