JavaScript 如何使用包含对象的数组并根据对象的属性进行检查

JavaScript 如何使用包含对象的数组并根据对象的属性进行检查

Array.includes()

方法: 在JavaScript中,includes()方法用于确定数组中是否存在特定元素。如果该元素存在,它将返回 true ,如果不存在,则返回 false

语法:

array_name.includes(searchElement, ?fromIndex)

参数:

  • searchElement: 要在数组中搜索的元素。
  • fromIndex: 要从中搜索元素的索引。这是一个可选参数。

示例:

HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<body> 
    <h2> 
        Checking if the countries  
        array contains Japan ---> 
        <span id="ans"></span> 
    </h2> 
    <h2> 
        Checking for Japan in the countries  
        array from index 2 ---> 
        <span id="ans2"></span> 
    </h2> 
  
    <script> 
        let countries = ["India", "Japan",  
            "Canada", "Germany", "Australia"]; 
  
        // 1st Output 
        let ans = document.querySelector("#ans"); 
        let output = countries.includes("Japan"); 
        ans.append(output); 
  
        // 2nd Output  
        let ans2 = document.querySelector("#ans2"); 
        let output2 = countries.includes("Japan", 2); 
        ans2.append(output2); 
    </script> 
</body> 
  
</html> 

输出:
JavaScript 如何使用包含对象的数组并根据对象的属性进行检查

1. 使用 in 运算符 如果属性存在于对象中,则返回true,如果不存在则返回false。它检查对象的自身属性和继承属性。

语法:

'property_name' in object_name

示例:

JavaScript

<script> 
    let Person = { 
        name: "durgesh", 
        age: 16 
    } 
      
    // Output: true 
    console.log('name' in Person) 
      
    // Returns true for an inherited 
    // property 
    // Output: true 
    console.log('toString' in Person) 
      
    // Output: false 
    console.log('gender' in Person) 
</script> 

输出:

true
true
false

注意: 上面示例中使用的toString()方法是从原型对象继承的属性。’in’ 运算符返回原型继承的属性的值为true。

使用hasOwnProperty() 方法: 如果属性存在于对象中,则返回true;如果不存在,则返回false。它仅检查“ 自有”属性(即在对象内部定义的属性)。

语法:

object_name.hasOwnProperty('property_name')

示例:

Javascript

<script> 
    let Person = { 
        name: 'Durgesh', 
        age: 16 
    }; 
      
    // Output: true 
    console.log(Person.hasOwnProperty('name')) 
      
    /* hasOwnProperty() doesn't checks for  
    inherited properties of the object. */
    /* toString() is an inherited property. */
    // Output: false 
    console.log(Person.hasOwnProperty('toString')); 
      
    // Output: false 
    console.log(Person.hasOwnProperty('gender')); 
</script> 

输出:

true
false 
false

与undefined比较:

评估对象中不存在的属性会返回undefined。因此,我们可以将结果与undefined进行比较,以确定属性是否存在或缺失。

示例:

Javascript

let Person = { 
    name: 'Durgesh', 
    age: 16 
}; 
  
// Returns true if the property is present 
// Output: true 
console.log(Person.name !== undefined) 
  
// Returns true for inherited property 
// Output: true 
console.log(Person.toString !== undefined) 
  
// Output: false 
console.log(Person.gender !== undefined)

输出:

true
true 
false

注意: 与上述两种方法相比,这种方法并不好,因为 如果对象中的属性被定义为undefined,则该方法会将其评估为false。 如果您的对象的属性值可能为undefined,建议使用上述两种方法。

Javascript

let Person = { 
    // Setting name to undefined 
    name: undefined, 
    age: 16 
}; 
  
/* This evaluates to false despite  
the fact that name property exists */
// Output: false 
console.log(Person.name!==undefined)

输出结果:

false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程