hasownproperty()
Menu hasownproperty Method
 

The hasownproperty() method

This page describes a usage of the hasownproperty() method.

The W3C Schools website describes the structure of a Javascript Object in terms of an Object having Properties and Methods. It does not explain the hasownprperty() method.

The W3C website does explain how a method is defined for an Object and it looks like hasownproperty() is an EMCAScript Object Function.

In my analysis of a JavaScript search of a page with a JSON object I started looking at some of the code that was used.

In the example I was lookig at the hasownproperty() method was used to determine the search term(s) were contained in a JSON list object.

As part of my continuing quest to explain the code I use and that I find in the process of its analysis the use of this method came to light with regards to searching data objects.

An example of the usage:

if(book.hasOwnProperty(key) && book[key] !== '') {
     bookString += book[key].toString().toLowerCase().trim() + ' ';
   }

The code snippet above comes from the Page Search linked above and I have tried to deseminate it here

Top

MDN say:

" The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it). "

In any case, this is a way of determining the contents of an object. How this differs from a search as a Regular Expression I need to investigate it.

This section needs to be re-formatted........

Filtering the book list

Finding the search string in the JSON object and re-rendering

  var handleSearch = function(event) {
    event.preventDefault();
    // Get the search terms from the input field
    var searchTerm = event.target.elements['search'].value;
    // Tokenize the search terms and remove empty spaces
    var tokens = searchTerm
                  .toLowerCase()
                  .split(' ')
                  .filter(function(token){
                    return token.trim() !== '';
                  });
   if(tokens.length) {
    //  Create a regular expression of all the search terms
    var searchTermRegex = new RegExp(tokens.join('|'), 'gim');
    var filteredList = books.filter(function(book){
      // Create a string of all object values
      var bookString = '';
      for(var key in book) {
        if(book.hasOwnProperty(key) && book[key] !== '') {
          bookString += book[key].toString().toLowerCase().trim() + ' ';
        }
      }
      // Return book objects where a match with the search regex if found
      return bookString.match(searchTermRegex);
    });
    // Render the search results
    render(filteredList);
   }
  };

Background to this page

This is one of the areas that MDN has more information than W3C, a search for this on W3C just results in a link to Javascript Objects.

I feel I need to learn a lot more about ECMAScripting.

This page is being revised as I see that there are others looking for more information on hasownproperty().

Links

Top

External Links - references

  • 1 - JavaScript Objects - https:// www.w3schools.com/js/ js_objects.asp - Real Life Objects, Properties, and Methods
  • 2 - Object.prototype.hasOwnProperty() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
  • 3 - Regular Expressions - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  • Regular_Expressions - 🔗 - MDN
  • 4 - JavaScript Objects - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND
  • 5 - The JavaScript this Keyword - https://www.w3schools.com/js/js_this.asp
  • 6 - && - Logical AND - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND
  • 7 - Regular expression - https://en.wikipedia.org/wiki/Regular_expression

Site design by Tempusfugit Web Design -