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

JavaScript中字符串與數(shù)組的includes()用法

 更新時(shí)間:2023年06月28日 19:01:48   投稿:mdxy-dxy  
這篇文章主要介紹了JavaScript中字符串與數(shù)組的includes()用法,需要的朋友可以參考下

字符串includes

查找字符串中是否包含指定的子字符串。

查找字符串是否包含 "Jb51":

定義和用法

includes() 方法用于判斷字符串是否包含指定的子字符串。

如果找到匹配的字符串則返回 true,否則返回 false。

注意: includes() 方法區(qū)分大小寫。

語法

string.includes(searchvalue, start)

參數(shù)值

參數(shù)描述
searchvalue必需,要查找的字符串。
start可選,設(shè)置從那個(gè)位置開始查找,默認(rèn)為 0。

返回值

類型描述
Boolean如果找到匹配的字符串返回 true,否則返回 false。

技術(shù)細(xì)節(jié)

JavaScript 版本:ECMAScript 6
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>腳本之家(jb51.net)</title>
</head>
<body>
<p>點(diǎn)擊按鈕查看檢測結(jié)果。</p>
<button onclick="myFunction()">點(diǎn)我</button>
<p id="demo"></p>
<p><strong>注意:</strong> IE 11 及更早版本不支持 includes() 方法 。</p>
<script>
function myFunction() {
  var str = "Hello world, welcome to the Jb51.";
  var n = str.includes("Jb51");
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>

數(shù)組includes

JavaScript Array includes() 方法

定義和用法

includes() 方法用來判斷一個(gè)數(shù)組是否包含一個(gè)指定的值,如果是返回 true,否則false。

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true

語法

arr.includes(searchElement)
arr.includes(searchElement, fromIndex)

參數(shù)說明

參數(shù)描述
searchElement必須。需要查找的元素值。
fromIndex可選。從該索引處開始查找 searchElement。如果為負(fù)值,則按升序從 array.length + fromIndex 的索引開始搜索。默認(rèn)為 0。

技術(shù)細(xì)節(jié)

返回值:布爾值。如果找到指定值返回 true,否則返回 false。
JavaScript 版本:ECMAScript 6

更多實(shí)例

fromIndex 大于等于數(shù)組長度

如果fromIndex 大于等于數(shù)組長度 ,則返回 false 。該數(shù)組不會(huì)被搜索:

var arr = ['a', 'b', 'c'];
arr.includes('c', 3);   //false
arr.includes('c', 100); // false

計(jì)算出的索引小于 0

如果 fromIndex 為負(fù)值,計(jì)算出的索引將作為開始搜索searchElement的位置。如果計(jì)算出的索引小于 0,則整個(gè)數(shù)組都會(huì)被搜索。

// 數(shù)組長度是3
// fromIndex 是 -100
// computed index 是 3 + (-100) = -97
var arr = ['a', 'b', 'c'];
arr.includes('a', -100); // true
arr.includes('b', -100); // true
arr.includes('c', -100); // true
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>腳本之家(jb51.net)</title>
</head>
<body>
<script>
let site = ['Jb51', 'google', 'taobao'];
document.write(site.includes('Jb51')); 
// true 
document.write("<br>"); 	
document.write(site.includes('baidu')); 
// false
</script>
</body>
</html>

到此這篇關(guān)于JavaScript中字符串與數(shù)組的includes()用法的文章就介紹到這了,更多相關(guān)字符串與數(shù)組的includes內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論