avatar
Published on

Lodash ๋ž€ ๐Ÿค”

Authors
  • avatar
    Name
    Haneul
    Twitter

Lodash

Lodash๋Š” ๋ฐฐ์—ด, ์ˆซ์ž, ๊ฐ์ฒด, ๋ฌธ์ž์—ด ๋“ฑ์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ๋ฒˆ๊ฑฐ ๋กœ์›€์„ ์—†์•  ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋” ์‰ฝ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. Lodash์˜ ๋ชจ๋“ˆ ์‹ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐฐ์—ด, ๊ฐ์ฒด ๋ฐ ๋ฌธ์ž์—ด ๋ฐ˜๋ณต
  • ๊ฐ’ ์กฐ์ž‘ ๋ฐ ํ…Œ์ŠคํŠธ
  • ๋ณตํ•ฉ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ

map

.map(collection, [iteratee=.identity])

  • ๋ฐฐ์—ด ์•ˆ์— ๊ฐ์ฒด๋“ค์˜ ์š”์†Œ ์ค‘, ํŠน์ • ์š”์†Œ๋งŒ ๋นผ์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with three arguments: (value, index|key, collection).

Many lodash methods are guarded to work as iteratees for methods like _.every, _.filter, _.map, _.mapValues, _.reject, and _.some.

Arguments collection (Array|Object): The collection to iterate over. [iteratee=_.identity] (Function): The function invoked per iteration.

Returns (Array): Returns the new mapped array.

Example

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']
Try in REPL

sortBy

  • Collection ๊ฐ’๋“ค์„ ์›ํ•˜๋Š” ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์ค๋‹ˆ๋‹ค.

.sortBy(collection, [iteratees=[.identity]])

Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratees are invoked with one argument: (value).

Arguments collection (Array|Object): The collection to iterate over. [iteratees=[_.identity]] (...(Function|Function[])): The iteratees to sort by.

Returns (Array): Returns the new sorted array.

Example

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 34 }
];

_.sortBy(users, [function(o) { return o.user; }]);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]

_.sortBy(users, ['user', 'age']);
// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]

uniqBy

  • ๋ฐฐ์—ด ์•ˆ์— ๊ฐ์ฒด๋“ค์˜ ์š”์†Œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (์ถ”๊ฐ€๋กœ, uniq ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.)

.uniqBy(array, [iteratee=.identity])

This method is like _.uniq except that it accepts iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed. The order of result values is determined by the order they occur in the array. The iteratee is invoked with one argument: (value).

Arguments array (Array): The array to inspect. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns (Array): Returns the new duplicate free array.

Example

_.uniqBy([2.1, 1.2, 2.3], Math.floor);
// => [2.1, 1.2]

// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

lodash tester codepen์—์„œ lodash๋ฅผ ํ…Œ์ŠคํŠธ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.