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?

More than 1 year has passed since last update.

【JavaScript 関数ドリル】中級編のdifferenceBy関数の実装アウトプット

Posted at

differenceBy関数の課題内容

_.differenceBy関数を自分で実装する課題。
https://lodash.com/docs/4.17.15#differenceBy

「課題内容」/「解説動画」/「解答例」を確認したい場合は、以下リンク先のページを参照。
https://js-drills.com/blog/differenceBy/

課題に取り組む前の状態

  • 解答を見なくてもできそうだと思った。

課題に取り組んだ後の状態

  • 解答例とは異なるが、サンプル通りの実行結果が出るように実装できた。

differenceBy関数の実装コード(答えを見る前)


function differenceBy(array, values, iteratee) {
    const copiedArray = [...array];
    
    for (let i = 0; i < values.length; i++) {
        console.log(`values[i] : ${values[i]}`);
        for (let j = copiedArray.length - 1; j >= 0; j--) {
            if (iteratee(values[i]) === iteratee(copiedArray[j])) {
                copiedArray.splice(j, 1);
            }
        }

    }
    return copiedArray;
}

console.log(differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor));
// => [1.2]

differenceBy関数の実装コード(答えを見た後)

// 同じ
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?