[Vue.js/라이브러리] lodash 사용해보기(4) - Collection
출처
Lodash Documentation
_(value) source Creates a lodash object which wraps value to enable implicit method chain sequences. Methods that operate on and return arrays, collections, and functions can be chained together. Methods that retrieve a single value or may return a primiti
lodash.com
◽ _.countBy(collection, [iteratee=_.identity])
_.countBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': 1, '6': 2 }
_.countBy(['one', 'two', 'three'], 'length');
// => { '3': 2 '5': 1 }
◽ _.each -> _.forEach
_.forEach([1, 2], function(value) { console.log(value); });
// => 1, 2
_.forEach({'a': 1, 'b': 2}, function(value, key) { console.log(key); });
// => 'a', 'b'
◽ _.eachRight -> _.forEachRight
_.forEach와 동일하나 마지막 인덱스부터 시작하는 것이 다르다.
◽ _.every(collection, [predicate=_.identity])
== 추후 정리
◽ _.filter(collection, [predicate=_.identity])
조건에 해당되는 객체를 배열로 만들어 반환한다.
let users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'travis', 'age': 37, 'active': true}
];
_.filter(users, function(o) { return !o.active; });
// => Objects for ['fred'], ['travis']
_.filter(users, ['active', false]);
// => Objects for ['fred'], ['travis']
_.filter(users, { 'age': 36, 'active': false });
// => obejects for []
◽ _.find(collection, [predicate=_.identity], [fromIndex=0])
조건에 해당되는 객체가 하나라도 있으면 종료한다.
만족하는 객체가 없는 경우, underfined를 반환한다.
◽ _.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1])
뒤에서부터 찾는다.
◽ _.flatMap(collection, [iteratee=_.identity])
_.flatMap([1, 2], (n) => { return [n, n]; });
// => [1, 1, 2, 2]
_.flatMap([1.4, 4.5], Math.floor);
// => [1, 4]
◽ _.flatMapDeep(collection, [iteratee=_.identity])_.flatMapDeep(collection, [iteratee=_.identity])
_.flatMapDeep([1, 2], (n) => { return [[n, n]]; });
// => [1, 1, 2, 2]
◽ _.flatMapDepth(collection, [iteratee=_.identity], [depth=1])
function duplicate(n) {
return [[[n, n]]];
}
_.flatMapDepth([1, 2], duplicate);
// => [[[1, 1]], [[2, 2]]]
_.flatMapDepth([1, 2], duplicate, 2);
// => [[1, 1], [2, 2]]
_.flatMapDepth([1, 2], duplicate, 3);
// => [1, 1, 2, 2]
◽ _.groupBy(collection, [iteratee=_.identity])
두번째 파라미터에 작성된 기준을 토대로 그룹을 형성하여 반환한다.
_.groupBy([6.1, 4.2, 6.5], Math.floor);
// => { '4': [4, 2], '6': [6.1, 6.3] }
_.groupBy(['one', 'two' 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }
◽ _.includes(collection, value, [fromIndex=0])
_.includes([1, 2, 3], 1);
// => true
// 첫번째 인덱스부터 검색하여 숫자 1이 존재하는가?
_.includes([1, 2, 3], 1, 2);
// => false
◽ _.invokeMap(collection, path, [args])
_.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
// => [[1, 5, 7], [1, 2, 3]]
_.invokeMap([123, 456], String.prototype.split, '');
// => [['1', '2', '3'], ['4', '5', '6']]
◽ _.keyBy(collection, [iteratee=_.identity])
◽ _.map
◽ _.orderBy
◽ _.partition
◽ _.reduce
◽ _.reduceRight
◽ _.reject
◽ _.sample
◽ _.sampleSize
◽ _.shuffle
◽ _.size
◽ _.some
◽ _.sortBy