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
MDN say:
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().