17
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

lodash でよく使う王道関数

Last updated at Posted at 2019-08-18

JavaScript を使っているなら lodash は大変便利なのですが、関数が多すぎてどれ使ったらいいのか解りづらいのが難点です。

ここではその中から特によく使う関数を紹介します。

_.chunk(array, [size=1]) : 配列を指定サイズで分割

// 配列を指定サイズで分割
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]

_.difference(array, [values]) : 差集合。つまり左から右を除去。

_.difference([2, 1], [2, 3]);
// => [1]

_.intersection([arrays]) : 積集合。つまり全てに共通してあるもののみ。

_.intersection([2, 1], [2, 3]);
// => [2]

_.union([arrays]) : 和集合。つまり全ての合算(重複なし)。

_.union([2, 1], [2, 3]);
// => [2, 1, 3]

_.xor([arrays]) : 排他的論理和。つまり全ての中で重複の無い値のみ。

_.xor([2, 1], [2, 3]);
// => [1, 3]

_.fill(array, value, [start=0], [end=array.length]) : 配列を値で埋める。

var array = [1, 2, 3];
_.fill(array, 'a');
console.log(array);
// => ['a', 'a', 'a']

_.uniq(array) : 重複排除。

_.uniq([2, 1, 2]);
// => [2, 1]

_.isEnpty(value) : 空判定。

_.empty({});
// => true
_.empty({a: 1});
// => false

lodash関連の記事

17
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?