日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js判断一个数组是否包含一个指定的值

發布時間:2025/6/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js判断一个数组是否包含一个指定的值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天看了一下 ?有好幾種方法 ?總結一下

1:array.indexOf ? 此方法判斷數組中是否存在某個值,如果存在返回數組元素的下標,否則返回-1

let arr = ['something', 'anything', 'nothing', 'anything']; let index = arr.indexOf('nothing'); console.log(index) //結果是2

2. array.includes(searchElement[, fromIndex]) ??此方法判斷數組中是否存在某個值,如果存在返回 true,否則返回false。

function test(fruit) {

const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];

if (redFruits.includes(fruit)) {
console.log('red');
}else{
console.log('blue');
}
}

test('aple')//結果是red

3. array.find(callback[, thisArg]) ? ??返回數組中滿足條件的第一個元素的值,如果沒有,返回undefined

// ---------- 元素是普通字面值 ---------- let numbers = [12, 5, 8, 130, 44]; let result = numbers.find(item => { return item > 8; });
console.log(result) # 結果: 12 // ---------- 元素是對象 ---------- let items = [ {id: 1, name: 'something'}, {id: 2, name: 'anything'}, {id: 3, name: 'nothing'}, {id: 4, name: 'anything'} ]; let item = items.find(item => { return item.id == 3; });
console.log(
item) # 結果: Object { id: 3, name: "nothing" }

4. array.findIndex(callback[, thisArg]) ?返回數組中滿足條件的第一個元素的索引(下標), 如果沒有找到,返回-1 ?同第3種方法類似

轉載于:https://www.cnblogs.com/GarsonZhang/p/10460195.html

總結

以上是生活随笔為你收集整理的js判断一个数组是否包含一个指定的值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。