[TIL] underscore.js(2)

박성진·2021년 2월 13일
0

._every
._every(list, [predicate])
list: collection으로써, 배열이나 객체가 될 수 있다.
[predicate]: list의 각 element(value)의 결과값이 truth인지 확인하는 test 함수이다.(생략가능)
-> list의 각 element(value)를 [predicate]함수로 돌려, 모든 element(value)가 통과될 경우, truth를 리턴한다. 만약 한 개의 element(value)라도 통과를 못하면 false를 리턴한다.

함수구현

_.every = function (arr, iteratee) {
  let result = true;

  _.each(arr, function (el) {
    if (iteratee === undefined) {
      if (Boolean(el) === false) {
        result = false;
      }
    } else if (Boolean(iteratee(el)) === false) {
      result = false;
    }
  })
  return result;
};

._pluck
._pluck(list, propertyName)
list: collection으로써, 배열이나 객체가 될 수 있다.
propertyName: 객체의 key 값이다.
-> list에서 propertyName이 가지고 있는 모든 value르 배열 형태로 리턴한다.

함수구현

_.pluck = function (arr, keyOrIdx) {
  return _.map(arr, function(el) {
    return el[keyOrIdx]
  })
};

._map
._map(list, iteratee)
list: collection으로써, 배열이나 객체가 될 수 있다.
iteratee: list의 각 element(value)를 변형시키는 함수이다.
-> list의 각 element(value)를 iteratee 함수를 돌려, 값을 변형시키고, 변형된 값을 배열의 형태로 리턴한다.

함수구현

_.map = function (arr, iteratee) {
  let result = [];

  _.each(arr, function(el) {
    result.push(iteratee(el))
  })
  return result;
};

0개의 댓글

Powered by GraphCDN, the GraphQL CDN