Sun Dial Menu
Menu hasownproperty Method

The hasownproperty() method

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


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) {
    // Get the search terms from the input field
    var searchTerm =['search'].value;
    // Tokenize the search terms and remove empty spaces
    var tokens = searchTerm
                  .split(' ')
                    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

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().



Top Menu

Site design by Tempusfugit Web Design -