Lodash 란?
30956 단어 JavaScriptlodashJavaScript
lodash는 Javascript의 인기있는 라이브러리 중 하나이다.
나도 실제로 쓴 경험이 있어 자주 사용되는 method에 대해 포스팅할 예정이다.
Array method
1. findIndex()
- 형식 : .findIndex(array, [predicate=.identity], [fromIndex=0])
- 출력 : index number
- 주어진 판별 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스 반환.
만약, 만족하는 요소가 없으면 -1 반환
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0
// The `_.matches` iteratee shorthand.
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1
// The `_.matchesProperty` iteratee shorthand.
_.findIndex(users, ['active', false]);
// => 0
// The `_.property` iteratee shorthand.
_.findIndex(users, 'active');
2. flatten()
- 형식 : _.flatten(array)
- 다차원 배열 내의 요소를 출력
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]
3. remove()
- 형식 : .remove(array, [predicate=.identity])
- 출력 : 제거된 array
- 배열 내의 조건에 맞는 요소들을 제거한 후 반환
var array=[1,2,3,4];
var evens=remove(array,function(n){
return n%2==0;
});
console.log(array);
//-> [1,3]
console.log(evens);
//-> [2,4]
Collection 관련 method
1. every()
- 형식 : .every(collection, [predicate=.identity])
- 출력 : boolean 값
- 배열 안의 요소들의 값들을 비교하고 분석
_.every([true, 1, null, 'yes'], Boolean);
// => false
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': false }
];
// The `_.matches` iteratee shorthand. (줄임말은 반복한다)
_.every(users, { 'user': 'barney', 'active': false });
// => false
// The `_.matchesProperty` iteratee shorthand.
_.every(users, ['active', false]);
// => true
// The `_.property` iteratee shorthand.
_.every(users, 'active');
// => false
2. find()
- 형식 : .find(collection, [predicate=.identity], [fromIndex=0])
- 첫 번째 요소 값 반환
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
_.find(users, function(o) { return o.age < 40; });
// => object for 'barney'
// The `_.matches` iteratee shorthand.
_.find(users, { 'age': 1, 'active': true });
// => object for 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.find(users, ['active', false]);
// => object for 'fred'
// The `_.property` iteratee shorthand.
_.find(users, 'active');
// => object for 'barney'
3. filter()
- 형식 : .filter(collection, [predicate=.identity])
- 특정 조건을 만족하는 모든 요소를 추출
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.filter(users, function(o) { return !o.active; });
// => objects for ['fred']
// The `_.matches` iteratee shorthand.
_.filter(users, { 'age': 36, 'active': true });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']
// The `_.property` iteratee shorthand.
_.filter(users, 'active');
// => objects for ['barney']
4. map()
- 형식 : .map(collection, [iteratee=.identity])
- 출혁 : 배열 내의 모든 요소 가각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환
function square(n) {
return n * n;
}
_.map([4, 8], square);
// => [16, 64]
_.map({ 'a': 4, 'b': 8 }, square);
// => [16, 64] (iteration order is not guaranteed)
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
// The `_.property` iteratee shorthand.
_.map(users, 'user');
// => ['barney', 'fred']
5. forEach()
- 형식 : .forEach(collection, [iteratee=.identity])
- 배열의 값마다 함수를 실행
_([1, 2]).forEach(function(n) {
console.log(n);
}).value();
// 1
// 2
6. includes()
- 형식 : _.includes(collection, value, [fromIndex=0])
- 출력 : boolean
- 해당 collection에 target값이 있는지 판별
// 배열에 값이 있는지 찾습니다.
_.includes([1, 2, 3], 1);
// → true
// index에 해당 값이 있는지 찾습니다.
_.includes([1, 2, 3], 1, 2);
// → false
// 일치하는 값이 있는지 찾습니다.
_.includes({ 'name': 'yhs', 'age': 26 }, 'yhs');
// → true
// 일치하는 값이 문자열 안에 있는지 찾습니다.
_.includes('dontknow', 'ont');
// → true
7. reduce (이해가 잘 가지 않아 좀 더 봐야 할 듯..)
- 형식 : .reduce(collection, [iteratee=.identity], [accumulator])
_.reduce([1, 2], function(sum, n) {
return sum + n;
}, 0);
// => 3
_.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
return result;
}, {});
// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)
참고
Author And Source
이 문제에 관하여(Lodash 란?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@wheezy_han/Lodash-란저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)