在JavaScript中, 我们可以使用3种方法来检查变量是否为数组:isArray方法, 使用运算符实例和使用检查构造函数类型如果它与Array对象匹配。
方法1:使用isArray方法
Array.isArray()方法检查传递的变量是否为Array对象。
语法如下:
Array.isArray(variableName)
如果变量是数组, 则返回布尔值, 如果不是, 则返回false。在下面的示例中显示。
示例1:
<!DOCTYPE html>
<html lang = "en">
<head>
<title>
How to check if a variable
is an array in JavaScript?
</title>
</head>
<body>
<h1 style = "color: green">
srcmini
</h1>
<b>
How to check if a variable
is an array in JavaScript?
</b>
<p>
Click on the button to check
if the variable is an array
</p>
<p>Output for string:
<div class = "outputString">
</div>
<p>Output for number:
<div class = "outputNumber">
</div>
<p>Output for array:
<div class = "outputArray">
</div>
<button onclick = "checkArray()">
Click here
</button>
<script type = "text/javascript">
function checkArray() {
let str = 'This is a string';
let num = 25;
let arr = [10, 20, 30, 40];
ans = Array.isArray(str);
document.querySelector(
'.outputString').textContent = ans;
ans = Array.isArray(num);
document.querySelector(
'.outputNumber').textContent = ans;
ans = Array.isArray(arr);
document.querySelector(
'.outputArray').textContent = ans;
}
</script>
</body>
</html>
输出如下:
方法2:使用instanceof运算符
instanceof运算符用于测试构造函数的prototype属性是否出现在对象的原型链中的任何位置。这可用于评估给定变量是否具有”数组”的原型。
语法如下:
variable instanceof Array
如果变量与所指定的变量(此处为数组)相同, 则运算符返回布尔值true, 否则返回false。在下面的示例中显示。
示例2:
<!DOCTYPE html>
<html lang = "en">
<head>
<title>
How to check if a variable is
an array in JavaScript?
</title>
</head>
<body>
<h1 style = "color: green">
srcmini
</h1>
<b>
How to check if a variable is
an array in JavaScript?
</b>
<p>
Click on the button to check
if the variable is an array
</p>
<p>Output for string:
<div class = "outputString"></div>
<p>Output for number:
<div class = "outputNumber"></div>
<p>Output for array:
<div class = "outputArray"></div>
<button onclick = "checkArray()">Click here</button>
<script type = "text/javascript">
function checkArray() {
let str = 'This is a string';
let num = 25;
let arr = [10, 20, 30, 40];
ans = str instanceof Array;
document.querySelector(
'.outputString').textContent =
ans;
ans = num instanceof Array;
document.querySelector(
'.outputNumber').textContent =
ans;
ans = arr instanceof Array;
document.querySelector(
'.outputArray').textContent =
ans;
}
</script>
</body>
</html>
输出如下:
方法3:检查变量的构造函数属性
检查变量的另一种方法是数组, 方法是使用Array检查其构造函数。
语法如下:
variable.constructor === Array
如果变量与指定的变量(此处为数组)相同, 则为true, 否则为false。在下面的示例中显示。
示例3:
<!DOCTYPE html>
<html lang = "en">
<head>
<title>
How to check if a variable is
an array in JavaScript?
</title>
</head>
<body>
<h1 style = "color: green">
srcmini
</h1>
<b>How to check if a variable is
an array in JavaScript?</b>
<p>Click on the button to check
if the variable is an array</p>
<p>Output for string:
<div class = "outputString"></div>
<p>Output for number:
<div class = "outputNumber"></div>
<p>Output for array:
<div class = "outputArray"></div>
<button onclick = "checkArray()">
Click here
</button>
<script type = "text/javascript">
function checkArray() {
let str = 'This is a string';
let num = 25;
let arr = [10, 20, 30, 40];
ans = str.constructor === Array;
document.querySelector(
'.outputString').textContent = ans;
ans = num.constructor === Array;
document.querySelector(
'.outputNumber').textContent = ans;
ans = arr.constructor === Array;
document.querySelector(
'.outputArray').textContent = ans;
}
</script>
</body>
</html>
输出如下:
来源:
https://www.srcmini02.com/67974.html