LoginSignup
40
24

More than 3 years have passed since last update.

lodashの配列/オブジェクト系関数の索引を作ってみた

Last updated at Posted at 2019-08-25

lodash のArray/Object/Collection系の関数は非常に多く、どれがどれか混乱しがち。これらを探しやすくするために、索引として使えるように分類してみました。

※1: 渡された配列/オブジェクト自体を変更しない
※2: 渡された配列/オブジェクト自体を変更する

[Array] 分割・結合 [※1]

  • chunk : 指定サイズで分割 _.chunk(array, [size=1])
  • concat : 配列結合 _.concat(array, [values])

[Array] 除去 [※1]

  • compact : false 的な値除去 _.compact(array)
  • drop : 先頭n個撤去 _.drop(array, [n=1])
  • dropRight : 末尾n個撤去 _.dropRight(array, [n=1])
  • dropWhile : 先頭から評価成立してる限り撤去 _.dropWhile(array, [predicate=_.identity])
  • dropRightWhile : 末尾から評価成立してる限り撤去 _.dropRightWhile(array, [predicate=_.identity])
  • without : values に合致する値撤去 _.without(array, [values])

[Array] 除去 [※2]

  • pull : values に合致する値撤去 _.pull(array, [...values])
  • pullAt : 指定の場所撤去 _.pullAt(array, [indexes])
  • remove : 合致するものを撤去 _.remove(array, [predicate=_.identity])

[Array] 集合 [※1]

  • difference : 差集合 _.difference(array, [values])
  • differenceBy : 差集合 _.differenceBy(array, [values], [iteratee=_.identity])
  • differenceWith : 差集合 _.differenceWith(array, [values], [comparator])
  • intersection : 積集合 _.intersection([arrays])
  • intersectionBy : 積集合 _.intersectionBy([arrays], [iteratee=_.identity])
  • intersectionWith : 積集合 _.intersectionWith([arrays], [comparator])
  • union : 和集合 _.union([arrays])
  • unionBy : 和集合 _.unionBy([arrays], [iteratee=_.identity])
  • unionWith : 和集合 _.unionWith([arrays], [comparator])
  • xor : 排他的論理和 _.xor([arrays])
  • xorBy : 排他的論理和 _.xorBy([arrays], [iteratee=_.identity])
  • xorWith : 排他的論理和 _.xorWith([arrays], [comparator])

[Array] 集合 [※2]

  • pullAll : 差集合 _.pullAll(array, values)
  • pullAllBy : 差集合 _.pullAllBy(array, values, [iteratee=_.identity])
  • pullAllWith : 差集合 _.pullAllWith(array, values, [comparator])

[Array] 埋める [※2]

  • fill : start から end(含まない) まで value で埋める _.fill(array, value, [start=0], [end=array.length])

[Array] 検索 [※1]

  • findIndex : predicate が true になる最初の index を返す _.findIndex(array, [predicate=_.identity], [fromIndex=0])
  • findLastIndex : 上記の逆順 _.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])
  • indexOf : 指定の値が存在する最初の最初の index を返す _.indexOf(array, value, [fromIndex=0])
  • lastIndexOf : 上記の逆順 _.lastIndexOf(array, value, [fromIndex=array.length-1])

[Object] 検索 [※1]

  • findKey : find だが返すのはキー _.findKey(object, [predicate=_.identity])
  • findLastKey : 上記の逆順 _.findLastKey(object, [predicate=_.identity])

[Array] ソートされてる前提での高速検索 [※1]

  • sortedIndex : insert されることになる index を返す _.sortedIndex(array, value)
  • sortedIndexBy : insert されることになる index を返す _.sortedIndexBy(array, value, [iteratee=_.identity])
  • sortedLastIndex : 上記の逆順 _.sortedLastIndex(array, value)
  • sortedLastIndexBy : 上記の逆順 _.sortedLastIndexBy(array, value, [iteratee=_.identity])
  • sortedIndexOf : indexOf _.sortedIndexOf(array, value)
  • sortedLastIndexOf : 上記の逆順 _.sortedLastIndexOf(array, value)

[Array] 平たん化 [※1]

  • flatten : 1段階平たん化 _.flatten(array)
  • flattenDeep : 再帰的に完全に平たん化 _.flattenDeep(array)
  • flattenDepth : 指定段階で平たん化 _.flattenDepth(array, [depth=1])

[Array/Object] ペア/Zip [※1]

  • fromPairs : [['a', 1], ['b', 2]]{ 'a': 1, 'b': 2 } _.fromPairs(pairs)
  • zipObject : ['a', 1], ['b', 2]{ 'a': 1, 'b': 2 } _.zipObject([props=[]], [values=[]])
  • toPairs : { 'a': 1, 'b': 2 }[['a', 1], ['b', 2]] _.toPairs(object)
  • toPairsIn : toPairs ただし、親も見る _.toPairsIn(object)
  • zipObjectDeep : プロパティパスを使える。['a.b[0].c', 'a.b[1].d'], [1, 2] _.zipObjectDeep([props=[]], [values=[]])
  • unzip : [['a', 1, true], ['b', 2, false]][['a', 'b'], [1, 2], [true, false]] _.unzip(array)
  • unzipWith : 上記のwith版 _.unzipWith(array, [iteratee=_.identity])
  • zip : ['a', 'b'], [1, 2], [true, false][['a', 1, true], ['b', 2, false]] _.zip([arrays])
  • zipWith : 上記のwith版 _.zipWith([arrays], [iteratee=_.identity])

[Array] 取り出す [※1]

  • head : 最初の1個 _.head(array)
  • last : 最後の1個 _.last(array)
  • take : 最初からn個 _.take(array, [n=1])
  • takeRight : 最後からn個 _.takeRight(array, [n=1])
  • takeWhile : 最初から一致する限り返す _.takeWhile(array, [predicate=_.identity])
  • takeRightWhile : 最後から一致する限り返す _.takeRightWhile(array, [predicate=_.identity])
  • initial : 末尾以外を返す _.initial(array)
  • tail : 先頭以外を返す _.tail(array)
  • nth : 指定番目の値。マイナスも使える _.nth(array, [n=0])

[Array] ユニーク [※1]

  • uniq : 重複なくす _.uniq(array)
  • uniqBy : 上記のby版 _.uniqBy(array, [iteratee=_.identity])
  • uniqWith : 上記のwith版 _.uniqWith(array, [comparator])
  • sortedUniq : ソートされている配列に最適化された uniq _.sortedUniq(array)
  • sortedUniqBy : 上記のby版 _.sortedUniqBy(array, [iteratee])

[Collection] グルーピング [※1]

  • countBy : グループごとカウント _.countBy(collection, [iteratee=_.identity])
  • groupBy : グルーピング _.groupBy(collection, [iteratee=_.identity])
  • keyBy : グルーピング。キーを計算で出す。キーが重複したらどれか1個 _.keyBy(collection, [iteratee=_.identity])

[Collection] 判定

  • every : すべてにマッチするか _.every(collection, [predicate=_.identity])
  • includes : 値として含まれてるか _.includes(collection, value, [fromIndex=0])
  • some : いずれかにマッチするか _.some(collection, [predicate=_.identity])

[Collection] 絞り込み [※1]

  • filter : 通常の filter に加え、shorthand 使える _.filter(collection, [predicate=_.identity])
  • find : 最初の1件 _.find(collection, [predicate=_.identity], [fromIndex=0])
  • findLast : 最後の1件 _.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1])
  • reject : filter の逆 _.reject(collection, [predicate=_.identity])

[Collection] ループ [※1]

  • forEach : オブジェクトでも使える。length があると配列としてループする。return false で break _.forEach(collection, [iteratee=_.identity])
  • forEachRight : 上記の逆順 _.forEachRight(collection, [iteratee=_.identity])

[Object] オブジェクト専用ループ [※1]

  • forIn : オブジェクト専用 forEach 。length の影響受けない。親クラスのプロパティも対象 _.forIn(object, [iteratee=_.identity])
  • forInRight : 上記の逆順 _.forInRight(object, [iteratee=_.identity])
  • forOwn : オブジェクト専用 forEach 。length の影響受けない。親クラスのプロパティは対象外 _.forOwn(object, [iteratee=_.identity])
  • forOwnRight : 上記の逆順 _.forOwnRight(object, [iteratee=_.identity])

[Collection] 各要素Map [※1]

  • map : map。オブジェクトでも使える _.map(collection, [iteratee=_.identity])
  • invokeMap : map。ただし引数与えられる _.invokeMap(collection, path, [args])
  • flatMap : map だが戻り値は flat になる(0~複数個を配列で返せる) _.flatMap(collection, [iteratee=_.identity])
  • flatMapDeep : map だが戻り値は flat になる(0~複数個を配列で返せる) _.flatMapDeep(collection, [iteratee=_.identity])
  • flatMapDepth : map だが戻り値は flat になる(0~複数個を配列で返せる) _.flatMapDepth(collection, [iteratee=_.identity], [depth=1])

[Object] 各要素Map [※1]

  • mapKeys : キー名を差し替えた object を生成するための map _.mapKeys(object, [iteratee=_.identity])
  • mapValues : 値を差し替えた object を生成するための map _.mapValues(object, [iteratee=_.identity])

[Collection] ソート [※1]

  • sortBy : ソート _.sortBy(collection, [iteratees=[_.identity]])
  • orderBy : sortBy。ただし asc/desc を指定できる _.orderBy(collection, [iteratees=[_.identity]], [orders])
  • shuffle : シャッフル _.shuffle(collection)

[Collection] 分割 [※1]

  • partition : 処理結果で2つに分割 _.partition(collection, [predicate=_.identity])

[Collection] reduce [※1]

  • reduce : iteratee の第1引数で前の値を受け取り、新しい値を返す。accumulator は整数でもなんでもいい。 _.reduce(collection, [iteratee=_.identity], [accumulator])
  • reduceRight : 上記の逆順 _.reduceRight(collection, [iteratee=_.identity], [accumulator])
  • transform : 基本 reduce。ただし、iteratee の第1引数で accumulator を受け取り、直接変更する。accumulator は配列か object 。ループを中断したいなら false を返す。 _.transform(object, [iteratee=_.identity], [accumulator])

[Collection] サンプリング [※1]

  • sample : 1件ランダム抽出 _.sample(collection)
  • sampleSize : n件ランダム抽出 _.sampleSize(collection, [n=1])

[Object] マージ [※2]

  • assign : Object.assign と同様。親クラスのプロパティは見ない。 _.assign(object, [sources])
  • assignWith : 上記のwith版 _.assignWith(object, sources, [customizer])
  • assignIn : Object.assign と同様。親クラスのプロパティを見る。 _.assignIn(object, [sources])
  • assignInWith : 上記のwith版 _.assignInWith(object, sources, [customizer])
  • defaults : Object.assign っぽいが、先勝ち _.defaults(object, [sources])
  • defaultsDeep : 上記のDeep版 _.defaultsDeep(object, [sources])
  • merge : .assign と同様。ただしDeep。 `.merge(object, [sources])`
  • mergeWith : 上記のwith版 _.mergeWith(object, sources, customizer)

[Object] Path [※1]

  • get : 値取得。なくてもエラーにならない。デフォルト値を返すようにもできる。 _.get(object, path, [defaultValue])
  • result : .get 。ただし、値が関数なら実行結果を返す `.result(object, path, [defaultValue])`
  • at : Pathでの取得結果を配列で _.at(object, [paths])
  • has : path が存在するか _.has(object, path)
  • hasIn : path が存在するか。親も見る _.hasIn(object, path)
  • invoke : path の値を関数として実行 _.invoke(object, path, [args])
  • omit : 無視する path を指定。親も見る _.omit(object, [paths])
  • omitBy : 上記のby版 _.omitBy(object, [predicate=_.identity])
  • pick : 活かすプロパティ(path)を指定 _.pick(object, [paths])
  • pickBy : 上記のby版 _.pickBy(object, [predicate=_.identity])

[Object] Path [※2]

  • set : path に値セット。中間のobjectやarrayがなければ作られる。 _.set(object, path, value)
  • setWith : 上記のwith版 _.setWith(object, path, value, [customizer])
  • unset : path のキー削除 _.unset(object, path)
  • update : path の値を updater の結果に更新 _.update(object, path, updater)
  • updateWith : 上記のwith版 _.updateWith(object, path, updater, [customizer])

[Object] キー/値抽出 [※1]

  • keys : キーを得る _.keys(object)
  • keysIn : キーを得る。親も見る _.keysIn(object)
  • functions : 関数名の配列を返す。親クラスは見ない _.functions(object)
  • functionsIn : 関数名の配列を返す。親クラスも見る _.functionsIn(object)
  • values : 値を得る _.values(object)
  • valuesIn : 値を得る。親も見る _.valuesIn(object)

[Object] 倒置 [※1]

  • invert : キー、値の入れ替え _.invert(object)
  • invertBy : 上記のby版 _.invertBy(object, [iteratee=_.identity])

lodash関連の記事

40
24
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
40
24