一、判斷對(duì)象是否包含某個(gè)屬性
可以使用以下幾種方法來(lái)判斷對(duì)象是否包含某個(gè)屬性:
1. in 操作符:
使用 in
操作符可以檢查對(duì)象是否包含指定的屬性。它會(huì)檢查對(duì)象及其原型鏈上的所有屬性。
const obj = { name: 'John', age: 25 };
console.log('name' in obj); // 輸出: true
console.log('gender' in obj); // 輸出: false
2. hasOwnProperty() 方法:
hasOwnProperty()
是對(duì)象的內(nèi)置方法,用于檢查對(duì)象自身是否具有指定的屬性(不包括原型鏈上的屬性)。
const obj = { name: 'John', age: 25 };
console.log(obj.hasOwnProperty('name')); // 輸出: true
console.log(obj.hasOwnProperty('gender')); // 輸出: false
3. 使用 undefined 進(jìn)行判斷:
通過訪問對(duì)象的屬性并與 undefined
進(jìn)行比較,可以判斷對(duì)象是否包含該屬性。
但當(dāng) obj 為null 或 undefined 時(shí)會(huì)報(bào)錯(cuò)。
const obj = { name: 'John', age: 25 };
console.log(obj.name !== undefined); // 輸出: true
console.log(obj.gender !== undefined); // 輸出: false
4. 使用 Object.keys() 方法:
Object.keys()
方法返回一個(gè)包含對(duì)象自身可枚舉屬性的數(shù)組。您可以使用該方法來(lái)獲取對(duì)象的所有屬性,并判斷指定屬性是否存在于返回的數(shù)組中。
const obj = { name: 'John', age: 25 };
console.log(Object.keys(obj).includes('name')); // 輸出: true
console.log(Object.keys(obj).includes('gender')); // 輸出: false
備注:這些方法可以根據(jù)您的需求選擇使用,以判斷對(duì)象是否包含某個(gè)屬性。請(qǐng)注意,前三種方法在屬性值為 undefined
時(shí)也會(huì)返回 true
,而最后一種方法不會(huì)將 undefined
視為存在的屬性。
5. 使用 Reflect.has(obj , keyName) 方法:
Reflect.has(obj, name)
Reflect.has方法對(duì)應(yīng)name in obj里面的in運(yùn)算符。
如果Reflect.has()方法的第一個(gè)參數(shù)不是對(duì)象,會(huì)報(bào)錯(cuò)。
let obj = {
name: '再努力些吧',
age: 18,
work: '前端',
}
// 舊寫法
console.log('age' in obj);//true
console.log('sex' in obj);//false
// 新寫法
console.log(Reflect.has(obj, 'age'));//true
console.log(Reflect.has(obj, 'sex'));//false
6、propertyIsEnumerable() 相當(dāng)于 hasOwnProperty() 的增強(qiáng)版
這個(gè)方法的用法與hasOwnProperty()相同,但當(dāng)檢測(cè)屬性是自有屬性(非繼承)且這個(gè)屬性是可枚舉的,才會(huì)返回true。
方便記憶可以理解為:
- in: 只要對(duì)象包含某個(gè)屬性就返回true, 包含原型鏈上的屬性
- hasOwnProperty: 首先滿足in, 其次屬性不屬于原型鏈
- propertyIsEnumerable: 首先滿足hasOwnProperty,其次該屬性未經(jīng)由Object.defineProperty定義為不可列舉
/* 如下例子我就不寫了引用別人的。作者:Netmad,來(lái)源:知乎,
鏈接:https://www.zhihu.com/question/21907133/answer/378501127 */
function foo() {
this.id = 'id';
}
foo.prototype.common = 'common';
var o = new foo();
'id' in o; // true
'common' in o; // true
'whatever' in o; // false
o.hasOwnProperty('id'); //true
o.hasOwnProperty('common'); //false
o.propertyIsEnumerable('id'); //true
o.propertyIsEnumerable('common'); //false
// 目前為止, hasOwnPerproty和propertyIsEnumerable看上去沒啥差別
// 通過Object.defineProperty定義新的屬性
Object.defineProperty(o, 'prop', {
value: 'valueOfProp',
enumerable: false
});
o.prop; // valueOfProp
o.hasOwnProperty('prop'); // true
o.propertyIsEnumerable('prop'); //false
// 如果defineProperty時(shí)enumerable為true, 那么這里依然和hasOwnProperty一樣
以上方法都可以判斷出對(duì)象是否包含某個(gè)屬性,工作中可以根據(jù)不同情況采用不同的方法。
二、判斷數(shù)組中是否包含某個(gè)值
可以使用以下幾種方法來(lái)判斷數(shù)組中是否包含某個(gè)值:
1. includes() 方法:
includes()
方法用于檢查數(shù)組是否包含指定的值,并返回一個(gè)布爾值。
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // 輸出: true
console.log(arr.includes(6)); // 輸出: false
2. indexOf() 方法:
indexOf()
方法返回指定值在數(shù)組中的第一個(gè)匹配項(xiàng)的索引,如果不存在則返回 -1。
const arr = [1, 2, 3, 4, 5];
console.log(arr.indexOf(3) !== -1); // 輸出: true
console.log(arr.indexOf(6) !== -1); // 輸出: false
3. find() 方法:
find()
方法返回?cái)?shù)組中滿足提供的測(cè)試函數(shù)的第一個(gè)元素的值,如果不存在則返回 undefined
。
const arr = [1, 2, 3, 4, 5];
console.log(arr.find(element => element === 3) !== undefined); // 輸出: true
console.log(arr.find(element => element === 6) !== undefined); // 輸出: false
4. some() 方法:
some()
方法測(cè)試數(shù)組中是否至少有一個(gè)元素通過了提供的測(cè)試函數(shù),返回一個(gè)布爾值。
const arr = [1, 2, 3, 4, 5];
console.log(arr.some(element => element === 3)); // 輸出: true
console.log(arr.some(element => element === 6)); // 輸出: false
備注:這些方法可以根據(jù)您的需求選擇使用,以判斷數(shù)組中是否包含某個(gè)值。請(qǐng)注意,前三種方法在比較值時(shí)使用的是嚴(yán)格相等運(yùn)算符(===
),而 some()
方法則通過測(cè)試函數(shù)來(lái)進(jìn)行判斷。
5. findIndex() 方法:
返回值:如果找到滿足條件的元素,則返回該元素的索引(大于等于 0);如果沒有找到滿足條件的元素,則返回 -1。
判斷方式:通過提供的測(cè)試函數(shù)對(duì)數(shù)組中的每個(gè)元素進(jìn)行判斷,直到找到滿足條件的元素為止。
示例:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-702705.html
const arr = [1, 2, 3, 4, 5];
console.log(arr.findIndex(element => element === 3)); // 輸出: 2
console.log(arr.findIndex(element => element === 6)); // 輸出: -1
以上5種都是ES6增加的。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-702705.html
到了這里,關(guān)于JS判斷對(duì)象、數(shù)組是否包含某個(gè)屬性、某個(gè)值的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!