Home Web Front-end JS Tutorial Collection of extended implementation codes for some Array and String prototype objects_javascript skills

Collection of extended implementation codes for some Array and String prototype objects_javascript skills

May 16, 2016 pm 06:14 PM
array string Expand

Some of the ones collected from Wuyou Script are really good and practical and worth collecting.
Methods to extend the prototype object of Array

Copy code The code is as follows:

//Delete data in the array
Array.prototype.del = function(n)
{
if (n<0) return this;
return this.slice(0,n ).concat(this.slice(n 1,this.length));
}
// Array shuffling
Array.prototype.random = function()
{
var nr =[], me=this, t;
while(me.length>0)
{
nr[nr.length] = me[t = Math.floor(Math.random() * me .length)];
me = me.del(t);
}
return nr;
}
//Number array sorting
Array.prototype.sortNum = function( f)
{
if (!f) f=0;
if (f==1) return this.sort(function(a,b){return b-a;});
return this.sort(function(a,b){return a-b;});
}
// Get the maximum item of the numeric array
Array.prototype.getMax = function()
{
return this.sortNum(1)[0];
}
// Get the minimum item of the numeric array
Array.prototype.getMin = function()
{
return this.sortNum (0)[0];
}
// The position where the specified element value first appears in the array
Array.prototype.indexOf = function(o)
{
for (var i =0; ireturn -1;
}
// Remove duplicate items in the array
Array.prototype.removeRepeat=function()
{
this.sort();
var rs = [];
var cr = false;
for (var i=0; i< ;this.length; i )
{
if (!cr) cr = this[i];
else if (cr==this[i]) rs[rs.length] = i;
else cr = this[i];
}
var re = this;
for (var i=rs.length-1; i>=0; i--) re = re.del (rs[i]);
return re;
}

Example:
var arr=["ni","wo","ta"];
Delete "wo" in the array
var newArr=arr.del(1);
Return the position where "me" first appears in the array, if not, return -1
var strPos=arr .indexOf("me");

Methods that extend the prototype object of String
Copy code The code is as follows:

//Get the character array
String.prototype.ToCharArray=function()
{
return this.split("");
}
//Get N identical strings
String.prototype.Repeat=function(num)
{
var tmpArr=[];
for(var i=0; ireturn tmpArr.join("");
}
//Reverse order
String.prototype.Reverse=function()
{
return this.split("").reverse().join("");
}
//Test whether it is a number
String.prototype.IsNumeric=function()
{
var tmpFloat=parseFloat(this);
if(isNaN(tmpFloat)) return false;
var tmpLen=this.length-tmpFloat.toString().length;
return tmpFloat " 0".Repeat(tmpLen)==this;
}
//Test whether it is an integer
String.prototype.IsInt=function()
{
if(this=="NaN ") return false;
return this==parseInt(this).toString();
}
// Combine multiple blanks into one blank
String.prototype.resetBlank = function()
{
return this.replace(/s /g," ");
}
// Remove left blank
String.prototype.LTrim = function()
{
return this.replace(/^s /g,"");
}
// Remove the right blank
String.prototype.RTrim = function()
{
return this. replace(/s $/g,"");
}
// Remove whitespace on both sides
String.prototype.trim = function()
{
return this.replace(/( ^s )|(s $)/g,"");
}
// Reserved numbers
String.prototype.getNum = function()
{
return this.replace( /[^d]/g,"");
}
// Reserved letters
String.prototype.getEn = function()
{
return this.replace(/[^ A-Za-z]/g,"");
}
// Keep Chinese
String.prototype.getCn = function()
{
return this.replace(/[ ^u4e00-u9fa5uf900-ufa2d]/g,"");
}
// Get the byte length
String.prototype.getRealLength = function()
{
return this.replace (/[^x00-xff]/g,"--").length;
}
//Truncate a string of specified length from the left
String.prototype.left = function(n)
{
return this.slice(0,n);
}
//Truncate a string of specified length from the right
String.prototype.right = function(n)
{
return this.slice(this.length-n);
}
// HTML encoding
String.prototype.HTMLEncode = function()
{
var re = this;
var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g];
var q2 = ["&","<",">"," "];
for(var i=0;ire = re.replace(q1[i],q2[i]);
return re;
}
//Unicode conversion
String.prototype.ascW = function()
{
var strText = "";
for (var i=0; ireturn strText;
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

How to use the Aurora Push extension to implement batch message push function in PHP applications How to use the Aurora Push extension to implement batch message push function in PHP applications Jul 25, 2023 pm 08:07 PM

How to use the Aurora Push extension to implement batch message push function in PHP applications. In the development of mobile applications, message push is a very important function. Jiguang Push is a commonly used message push service that provides rich functions and interfaces. This article will introduce how to use the Aurora Push extension to implement batch message push functionality in PHP applications. Step 1: Register a Jiguang Push account and obtain an API key. First, we need to register on the Jiguang Push official website (https://www.jiguang.cn/push)

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

See all articles