0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

size,each,map,filterを自分で書いてみる

Last updated at Posted at 2025-10-17

size

  • 与えられた引数の長さを返す
    配列かオブジェクトどちらでも判定する

_.size = (collection) => { if (Array.isArray(collection)) { return collection.length; }else { return Object.keys(collection).length; } };

each

  • 第二引数に渡された関数に、第一引数で渡された配列またはオブジェクトから
    取り出した要素、キー、配列、またはオブジェクト自身の3つを渡して実行する

_.each = (collection,iteratee) => { if(Array.isArray(collection){ for(let i = 0; i < collection.length; i++) { iteratee(collection[i],i,collection) }else { for(const key in collection){ iteratee(collection[key],key,collection); } } };

map

  • eachを使って、配列またはオブジェクトの要素に対して関数を実行し配列にプッシュする

_.map = (collection,iteratee) => { const resultArray = []; _.each(collection,(val) => { resultArray.push(iteratee(val)); }); return result; };

filter

  • eachを使って、配列またはオブジェクトの要素に対してtrue or falseを判定する関数を実行し、trueの時だけ配列にプッシュする

_.filter = (collection,test) => { const resultArray = []; _.each(collection,(val) => { if(test(val)) resultArray.push(val); });

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?