Javascript brute-force lookup method simplified version _ javascript skills

Source: Internet
Author: User
Tags key string
Anyone who has some experience knows that Jscript has limited efficiency after all. It is very slow to use regular algorithms to search for data in arrays. For example, in a data array containing 500 strings, we want to find a specified character (key) and return its array subscript. If this algorithm is used:
Function usual_search (data, key)
{
Var m = data. length
For (I = 0; I {If (data [I] = key) return I}
}
Because you need to compare multiple times, the operation will be quite slow.

This topic describes a method that makes full use of the built-in Jscript method to find data in the array. Thanks to the built-in Jscript method, the efficiency is far better than the conventional algorithm. For the sake of humor, I named it "JS abuse lookup ".

This search method requires an array element: the content of the array element cannot contain commas (,) and a designated replacement symbol (for example, in the following example, we specify the replacement symbol as a tab "delimiter "). Pay attention to this requirement when constructing and maintaining arrays in advance.

The idea of JS abuse lookup method is very simple. There is only one principle, that is, to "make full use of the built-in Jscript method ":

First, we use the toString () method of the Array object to generate a string containing Array elements. In this string, the Array elements are separated by commas, therefore, we require that the content of the array element should not contain commas.

Then, we use the replace () method of the String object to replace the key String we are looking for in this String with a special symbol (replacement symbol) We specified ), generally, select an uncommon character to fill the contemporary symbols. In the following example, I use a tab ), as long as the symbols that do not appear in the array element can be filled with contemporary symbols.

The next step is our most violent step. We still use the replace () method to remove all characters other than the half-angle comma (,) and the placeholder. After all the data is cleared, this string becomes a string of half-width commas containing a placeholder symbol (this looks like :,,,,,,,,,,,,,,,, break ,,,,,,,,,).

Finally, the indexOf () method of the String object is used to return the position of the substituted symbol in this String, which is exactly the array subscript in the original array.

Script function JS_cruel_search (data, key)/* JS brute-force search */{var splitstr = "<bound \ 0>" var t = (splitstr + data. join (splitstr) + splitstr ). split (splitstr + key + splitstr); if (t. length & (x = t [0]. split (splitstr ). length-1) <data. length) return x} function show () {p = DataWord. value. split (",") key = keyWord. value result = JS_cruel_search (p, key) if (result>-1) {alert ("+ key +" is in the "+ (result + 1) + "positions. ")} Else {alert (" not found! ")}" Script "Data: <input name = DataWord readonly value =" you, eyes, ends, ignorance, days, orders, pure, old, earning, squatting, not willing, involving, vulgar, Hongyi, borrow, his wings, wings, can, who can help, him, fly? "Size = 120> <p> key: <input name = keyWord value =" "> <p> <button onclick = show ()> Search </button>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.