亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

利用js判斷數(shù)據(jù)是否是數(shù)組或字符串的常見方法

 更新時(shí)間:2023年07月19日 09:52:58   作者:琉璃℡初雪  
這篇文章主要給大家介紹了關(guān)于利用js判斷數(shù)據(jù)是否是數(shù)組或字符串的常見方法,其實(shí)有很多方法可以判斷數(shù)據(jù)是否是數(shù)組或字符串,需要的朋友可以參考下

有很多方法可以判斷數(shù)據(jù)是否是數(shù)組或字符串,以下列舉一些常見的方法:

1.通過typeof操作符判斷數(shù)據(jù)類型,如果為“string”,則數(shù)據(jù)為字符串;如果為“object”,則需要進(jìn)一步判斷是否為數(shù)組。

function isStringOrArray(data) {
  if (typeof data === "string") {
    console.log("數(shù)據(jù)為字符串");
  } else if (typeof data === "object") {
    if (Array.isArray(data)) {
      console.log("數(shù)據(jù)為數(shù)組");
    }
  }
}

2.使用instanceof操作符判斷數(shù)據(jù)類型,如果為String,則數(shù)據(jù)為字符串;如果為Array,則數(shù)據(jù)為數(shù)組。

function isStringOrArray(data) {
  if (data instanceof String) {
    console.log("數(shù)據(jù)為字符串");
  } else if (data instanceof Array) {
    console.log("數(shù)據(jù)為數(shù)組");
  }
}

3.使用Array.isArray()方法判斷數(shù)據(jù)是否為數(shù)組。

 
function isStringOrArray(data) {
  if (Array.isArray(data)) {
    console.log("數(shù)據(jù)為數(shù)組");
  } else {
    console.log("數(shù)據(jù)為字符串");
  }
}

4.利用正則表達(dá)式判斷數(shù)據(jù)是否為字符串。

 
function isStringOrArray(data) {
  if (/^[a-zA-Z]+$/.test(data)) {
    console.log("數(shù)據(jù)為字符串");
  } else {
    console.log("數(shù)據(jù)為數(shù)組");
  }
}

總結(jié)

到此這篇關(guān)于利用js判斷數(shù)據(jù)是否是數(shù)組或字符串的常見方法的文章就介紹到這了,更多相關(guān)js判斷是數(shù)組或字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論