LoginSignup
5
5

More than 1 year has passed since last update.

JavaScriptで配列を比較する方法

Posted at

1.ソース

array.js
/**
 * 配列の比較
 * @param array1 配列1
 * @param array2 配列2
 * @returns 同じの場合:True、その他の場合:False
 */
export const arraysEqual = (array1, array2) => {
    if (!array1 || !array2) {
        return false;
    }

    if (array1.length !== array2.length) {
        return false;
    }

    for (let index = 0; index < array1.length; index++) {
        if (array1[index] instanceof Array && array2[index] instanceof Array) {
            if (!arraysEqual(array1[index], array2[index])) {
                return false;
            }
        } else if (array1[index] !== array2[index]) {
            return false;
        }
    }

    return true;
}
5
5
2

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