본문 바로가기

프로그래밍/Javascript

lodash 문법

Functional Javascript를 읽으면서 underscore 관련 문법들이 자세히 나와 있지 않아서 따로 정리해본다. 근데 서치해보니 요즘에는 lodash가 좀더 생산성이 좋다고 해서 lodash를 기준으로 삼았다.

_.pluck()

객체로 구성된 배열에서 객체의 특정 키에대한 값을 추출할 때 사용한다.

var users = [{
  id: 1,
  name: 'Chris'
}, {
  id: 2,
  name: 'Mike'
}, {
  id: 3,
  name: 'Sam'
}];

_.pluck(users, 'id');
// [1, 2, 3]

_.rest

Creates a function that invokes func with the this binding of the created function and arguments from start and beyond provided as an array.

생성된 함수 및 array형식으로 제공된 arguments들의 처음부터 끝까지를 this로 바인딩하는 함수를 유발시키는 함수를 만들어준다.

var update = function(fun, /* args */) {

var args = _.rest(arguments);

var oldValue = this._value

this._value = fun.apply(this, construct(oldValue, args));

return this._value;

}