Javascript 如何使用underscore的chain方法返回多维数组中的第一项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10640646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:17:18  来源:igfitidea点击:

How do you use underscore's chain method to return the first item in a multidimensional array?

javascriptunderscore.jschainingchain

提问by Chris Matta

Say I have an array of arrays, and I want to return the first element of each array within the array:

假设我有一个数组数组,我想返回数组中每个数组的第一个元素:

array = [[["028A","028B","028C","028D","028E"],
          ["028F","0290","0291","0292","0293"],
          ["0294","0295","0296","0297","0298"],
          ["0299","029A","029B","029C","029D"],
          ["029E","029F","02A0","02A1","02A2"]],
         [["02A3","02A4"],
          ["02A5", "02A6"]];

I know I can do something like this:

我知道我可以做这样的事情:

var firsts = [];
_.each(array, function(item){
  _.each(item, function(thisitem){
    firsts.push(_.first(thisitem));
  });
});

but what if I want to do it with underscore's _.chain()method? Just learning underscore, and so far seems very useful.

但是如果我想用下划线的_.chain()方法来做呢?刚刚学习下划线,目前看来很有用。

回答by mu is too short

You could do it with flattenand mapthusly:

你可以这样做flattenmap因此:

var firsts = _.chain(array)
              .flatten(true) // This true is important.
              .map(function(a) { return a[0] })
              .value();

Demo: http://jsfiddle.net/ambiguous/cm3CJ/

演示:http: //jsfiddle.net/ambiguous/cm3CJ/

You use flatten(true)to convert your array-of-arrays-of-arrays into an array-of-arrays and then the mappeels off the first element of each inner array.

您用于flatten(true)将数组数组转换为数组数组,然后map剥离每个内部数组的第一个元素。

If you want something shorter than the map, you could use pluckto pull out the first element of the inner arrays:

如果你想要比 短的东西map,你可以pluck用来拉出内部数组的第一个元素:

var firsts = _.chain(array)
              .flatten(true) // This true is important.
              .pluck(0)
              .value();

Demo: http://jsfiddle.net/ambiguous/pM9Hq/

演示:http: //jsfiddle.net/ambiguous/pM9Hq/

_.pluckis just a mapcall anyway:

_.pluckmap无论如何只是一个电话:

// Convenience version of a common use case of `map`: fetching a property.
_.pluck = function(obj, key) {
  return _.map(obj, function(value){ return value[key]; });
};

This one looks a lot more like the .map(&:first)that you'd use in Ruby so it might be more familiar to some people and more concise once you're used to pluck. If you really want something Rubyish, you could use a non-anonymous function with map:

这个看起来更像.map(&:first)你在 Ruby 中使用的 ,所以它可能对某些人更熟悉,一旦你习惯了它就会更简洁pluck。如果你真的想要一些 Rubyish,你可以使用一个非匿名函数map

var first  = function(a) { return a[0] };
var firsts = _.chain(array)
              .flatten(true) // This true is important.
              .map(first)
              .value();