LoginSignup
0
0

More than 3 years have passed since last update.

一部分が重複している2つの配列の、差分を取得する(JavaScript)

Posted at

一部分が重複している配列の差分を取得します(JavaScript)

sample.rb

function arr_diff (a1, a2) {

    var a = [], diff = [];

    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }

    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }

    for (var k in a) {
        diff.push(k);
    }

    return diff;
}

console.log(arr_diff(['a', 'b'], ['a', 'b', 'c', 'd']));
console.log(arr_diff("abcd", "abcde"));
console.log(arr_diff("zxc", "zxc"));

引用元では他の方法も記載されているようでした。

引用元
https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript

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