如何用JS判斷數(shù)組中是否存在某個(gè)值或者某個(gè)對(duì)象的值
一、判斷是否存在某個(gè)值
1、Array.prototype.indexOf()
indexOf()方法返回在數(shù)組中可以找到一個(gè)給定元素的第一個(gè)索引,如果不存在,則返回-1。
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
console.log(beasts.indexOf('giraffe'));
// expected output: -12、Array.prototype.includes()
includes() 方法用來(lái)判斷一個(gè)數(shù)組是否包含一個(gè)指定的值,根據(jù)情況,如果包含則返回 true,否則返回 false。
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('at'));
// expected output: false3、Array.prototype.find()
find() 方法返回?cái)?shù)組中滿足提供的測(cè)試函數(shù)的第一個(gè)元素的值。否則返回 undefined。
const array1 = [5, 12, 8, 130, 44]; const found = array1.find(element => element > 10); console.log(found); // expected output: 12
4、Array.prototype.findIndex()
findIndex()方法返回?cái)?shù)組中滿足提供的測(cè)試函數(shù)的第一個(gè)元素的索引。若沒(méi)有找到對(duì)應(yīng)元素則返回-1。
const array1 = [5, 12, 8, 130, 44]; const isLargeNumber = (element) => element > 13; console.log(array1.findIndex(isLargeNumber)); // expected output: 3
二、判斷是否存在對(duì)象的某個(gè)值
1、Array.prototype.find() 同上3
const arr = [{id:1, name:'name1'}, {id:2, name:'name2'}, {id:3, name:'name3'}];
const res = arr.find((ev) => {
return ev.id === 3;
});
console.log(res);
// expected output: { id: 3, name: "name3" }
const ret = arr.find((ev) => {
return ev.id === 4;
});
console.log(ret);
// expected output: undefined2、Array.prototype.findIndex() 同上4
const arr = [{id:1, name:'name1'}, {id:2, name:'name2'}, {id:3, name:'name3'}];
const res = arr.findIndex((ev) => {
return ev.id === 3;
});
console.log(res);
// expected output: 2
const ret = arr.findIndex((ev) => {
return ev.id === 4;
});
console.log(ret);
// expected output: -1補(bǔ)充:js判斷數(shù)組(數(shù)組對(duì)象)中是否存在指定的值,如果存在就刪除
數(shù)組中是否存在指定值,存在就刪除
var str = ["a", "b", "c"];
var index = str.indexOf("a");
if(index>-1){//大于0 代表存在,
str.splice(index,1);//存在就刪除
}
console.log(str);// ["b", "c"]
數(shù)組對(duì)象中是否存在指定值(方法一),存在即刪除
var searchinfo =[
{ key: '999', name: 'zhangsan'},
{ key: '111', name: 'lisi'},
{ key: '222', name: 'wanger'},
{ key: '333', name: 'apple'},
{ key: '444', name: 'orange'},
]
for (var i = 0; i < searchinfo.length; i++) {
if ((searchinfo[i].key).indexOf("999") > -1) {//判斷key為999的對(duì)象是否存在,
index = i;
searchinfo.splice(index, 1);//存在即刪除
}
}
console.log(searchinfo);運(yùn)行結(jié)果:

數(shù)組對(duì)象中是否存在指定值(方法二)
var searchinfo =[
{ key: '999', name: 'zhangsan'},
{ key: '111', name: 'lisi'},
{ key: '222', name: 'wanger'},
{ key: '333', name: 'apple'},
{ key: '444', name: 'orange'},
]
var str1 = searchinfo.some(item => item.key =="999");
console.log(str);//true 存在
var str2 = searchinfo.some(item => item.key =="121");
console.log(str);//false 不存在
總結(jié)
到此這篇關(guān)于如何用JS判斷數(shù)組中是否存在某個(gè)值或者某個(gè)對(duì)象的值的文章就介紹到這了,更多相關(guān)JS判斷數(shù)組存在某個(gè)值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解微信小程序?qū)崿F(xiàn)WebSocket心跳重連
這篇文章主要介紹了詳解微信小程序?qū)崿F(xiàn)WebSocket心跳重連,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
js獲取TreeView控件選中節(jié)點(diǎn)的Text和Value值的方法
在實(shí)際項(xiàng)目中,遇到一個(gè)問(wèn)題,首先彈出一個(gè)新窗口,新窗口中放了一個(gè)TreeView控件,現(xiàn)在要解決的是,如何單擊TreeView中一個(gè)節(jié)點(diǎn),返回Text和Value到父頁(yè)面并關(guān)閉該新窗口,本文將詳細(xì)介紹此方法的實(shí)現(xiàn)2012-11-11
Bootstrap整體框架之JavaScript插件架構(gòu)
這篇文章主要介紹了Bootstrap整體框架之JavaScript插件架構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
JS中Select下拉列表類(lèi)(支持輸入模糊查詢)功能
這篇文章主要介紹了JS中Select下拉列表類(lèi)(支持輸入模糊查詢)功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01
微信瀏覽器內(nèi)置JavaScript對(duì)象WeixinJSBridge使用實(shí)例
這篇文章主要介紹了微信瀏覽器內(nèi)置JavaScript對(duì)象WeixinJSBridge使用實(shí)例,本文給出了分享到朋友圈、發(fā)送給好友、分享到騰訊微博、關(guān)注指定的微信號(hào)等功能代碼,需要的朋友可以參考下2015-05-05

