Wednesday, July 3, 2024
HomeLanguagesJavascriptHow to use array that include and check an object against a...

How to use array that include and check an object against a property of an object ?

Array.includes() Method: In JavaScript, includes() method is used to determine that a particular element is present in an array or not. It returns true if the element is present and false when it is absent.

Syntax:

array_name.includes(searchElement, ?fromIndex)

Parameters:

  • searchElement: The element to be search in the array.
  • fromIndex: The index from which the element is to be searched. This is an optional parameter.

Example: 

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>


Output: Checking for a property in an Object: To check that an Object contains a particular property or not, we have 3 methods to do this-

1. Using in operator: It returns true if the property exists in the object and false if it doesn’t exists. It checks for both the own and inherited properties of the object.

Syntax:

'property_name' in object_name

Example:

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>


Output:

true
true
false

Note: The toString() method used in above example as an inherited property from prototype object. The ‘in’ operator returns true for prototype inherited properties. 

Using hasOwnProperty() Method: It returns true if the property exists in the object and false if it doesn’t exists. It checks only for ‘own’ properties(The properties that are defined inside the object) of the object.

Syntax:

object_name.hasOwnProperty('property_name')

Example:

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>


Output:

true
false 
false

Comparing with undefined: Evaluating a property that doesn’t exists in an object results in undefined. So we can compare the result with undefined to know that a property is present or absent. 

Example:

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)


Output:

true
true 
false

Note: This is an unpleasant approach compared to the above two because, if a property is defined as undefined in the object then this method evaluates it to false. It is advisable to use above two methods if there is a possibility that your object’s property value can be 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)


Output:

false

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments