2
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 5 years have passed since last update.

論理回路 JavaScript

Last updated at Posted at 2019-12-12

はじめに

差分関数を実装する機会がありましたので、覚書程度に。
適宜追加・修正しようと思っています。
includes()methodを使いましたが、indexOf()methodを使っても実装できるみたいですね。
訂正等あればコメントください。

結論

Web 1920 – 2.jpg

const logical_expression = function (arr1, arr2) {
    let intersection = arr1.filter(e => arr2.includes(e));
    console.log(intersection);
}

logical_expression([1, 2, 3], [2, 3, 5])
    // -> [ 2, 3 ]

Web 1920 – 3.jpg

const logical_expression = function (arr1, arr2) {
    let differenceA = arr1.filter(e => !arr2.includes(e));
    console.log(differenceA);
}
logical_expression([1, 2, 3], [2, 3, 5])
    // ->[ 1 ]

Web 1920 – 4.jpg

const logical_expression = function (arr1, arr2) {
    let differenceB = arr2.filter(e => !arr1.includes(e));
    console.log(differenceB);
}
logical_expression([1, 2, 3], [2, 3, 5])
    // ->[ 5 ]

Web 1920 – 5.jpg

const logical_expression = function (arr1, arr2) {
    let symmetric = arr1
        .filter(e => !arr2.includes(e))
        .concat(arr2.filter(e => !arr1.includes(e)));
    console.log(symmetric);
}
logical_expression([1, 2, 3], [2, 3, 5])
   // ->[ 1, 5 ]
2
0
1

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
2
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?