オブジェクトが配列であるかどうかを確認するJavaScriptプログラム

Javascript

例:Array.isArray()を使用して配列を確認する

// program to check if an object is an array

function checkObject(arr) {

    // check if arr is array
    const result = Array.isArray(arr);

    if(result) {
        console.log(`[${arr}] is an array.`);
    }
    else {
        console.log(`${arr} is not an array.`);
    }

}

const array = [1, 2, 3];

// call the function
checkObject(array);

出力

[1,2,3] is an array.

上記のプログラムでは、 Array.isArray() メソッドは、オブジェクトが配列であるかどうかを確認するために使用されます。

ザ・ Array.isArray() メソッドは true オブジェクトが配列の場合、それ以外の場合は false

注意:配列の場合、 typeof 演算子はオブジェクトを返します。

例えば、

const arr = [1, 2, 3];
console.log(typeof arr); // object



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です