는 :
// Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head` and `take`. The **guard** check
// allows it to work with `_.map`.
_.first = _.head = _.take = function(array, n, guard) {
if (array == null) return void 0;
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
};
가드 검사는 _.map
작업을 수행 할 수 있습니다.
그래서 당신은이 같은 배열이있는 경우 :
[ [], [4] ]
때문에 _.map
로가는 인수 : 이것은 당신이 같을 것이다 발생할 경우 아니었다면
var a = [ [1, 2, 3], [4, 5, 6] ];
// put this array though _.map and _.first
_.map(a, _.first); // [1, 4]
을 :
_.map(['a', 'b', 'c'], function(val, key, obj) {
// key = 0, 1, 2
// val = a, b, c
// obj = ['a', 'b', 'c']
// the obj argument is why `guard` is truly and the first element in the array is returned rater than using [].slice
});
,451,515,
아름다운하지만 그것은 함께 작업 할 수 없습니다 :
_.first([1, 2, 3], 2) // [1, 2]
_.first([1, 2, 3], 2, true) // 1
_.first([1, 2, 3], 2, 3) // 1
[표준] (http://www.ecma-international.org/ecma-262/5.1/#sec-15.4에 따르면. 4.19),'Array # map'은 * 3 개의 인수를 콜백에 전달합니다. 두 개가 아닌 "* callbackfn'은 요소의 값, 요소의 인덱스 및 가로 지르는 객체의 세 가지 인자로 호출됩니다. " – DCoder
@DCoder 이것은 정확하고 실제로'_.first' 함수가 첫 번째 값을 반환하는 이유입니다 :) Thanks – andlrc