A simple extended _javascript technique for array

Source: Internet
Author: User
is actually a simplified version of the enumeration object section in prototype 1.4,
First put the code, and so on one by one explanations, may be helpful to beginners

have been learning Python lately and have been lamenting the flexibility of the list in Python,
are also arrays, why can't JavaScript be as flexible as python?
(In fact, the following prototype 1.4 has been achieved, but prototype is too fat, you know, I'm not interested in fat women)
So we have the following code ...

Copy Code code as follows:

<script>
Array.prototype.map = function (f) {
var arr=[]
for (Var i=0;i<this.length;i++) Arr.push (f (this[i))
Return arr
}

Array.prototype.each = function (f) {
for (Var i=0;i<this.length;i++) F (this[i])
}

Array.prototype.find = function (f) {
for (Var i=0;i<this.length;i++) if (f (this[i])) return this[i]
return null
}

Array.prototype.findAll = function (f) {
var arr=[]
for (Var i=0;i<this.length;i++) if (f (this[i)) Arr.push (this[i))
Return arr
}

Array.prototype.getPart = function (f) {
for (Var i=0;i<this.length;i++) if (f (this[i])) return [This.slice (0,i+1), This.slice (i+1)]
}


Array.prototype.all = function (f) {
for (Var i=0;i<this.length;i++) if (!f (This[i)) return False
return True
}

Array.prototype.any = function (f) {
for (Var i=0;i<this.length;i++) if (f (this[i)) return True
return False
}

Array.prototype.grep = function (regex,f) {
var arr=[]
for (Var i=0;i<this.length;i++) {
if (This[i].tostring (). Match (regex)) Arr.push ((f | | function (x) {return x}) (This[i))
}
Return arr
}

Array.prototype.include = function (v) {
for (Var i=0;i<this.length;i++) if (this[i]==v) return True
return False
}
Array.prototype.indexOf = function (v) {
for (Var i=0;i<this.length;i++) if (this[i]==v) return I
Return-1
}

Array.prototype.inject = function (v,f) {
var vv=v
for (Var i=0;i<this.length;i++) vv=f (Vv,this[i])
return VV
}

Array.prototype.max=function () {
var v=this[0];
for (Var i=0;i<this.length;i++) if (this[i]>v) v=this[i]
Return V
}

Array.prototype.min=function () {
var v=this[0];
for (Var i=0;i<this.length;i++) if (this[i]<v) v=this[i]
Return V
}

Array.prototype.partition = function (f) {
var arr1=[],arr2=[]
for (Var i=0;i<this.length;i++) F (this[i])? Arr1.push (This[i): Arr2.push (this[i))
return [ARR1,ARR2]
}

</script>
<textarea id=o>
There are a lot of text and script blocks, and the number of script blocks is variable.
<script>alert (1) </script>
<script>
function FFF () {
Alert ("FFF function")
}
FFF () </script>
<script>alert ("Last Alert ()") </script>
</textarea>

<script>
Each element in the array calls function f execution and returns the result of the execution in the order of the array, returning an array
Array.prototype.map = function (f) {
var arr=[]
for (Var i=0;i<this.length;i++) Arr.push (f (this[i))
Return arr
}

Every element in an array calls function f execution, only executes, and does not return results
Array.prototype.each = function (f) {
for (Var i=0;i<this.length;i++) F (this[i])
}

The simplest example
var arr=[3,1,5,2,7]
var arr2=arr.map (function (x) {return x*x})
Alert (ARR2)

Take the whole
var arr3=[3.121,1.555,5.8,2.0,7.9]
var arr4=arr3.map (Math.Round)
Alert (ARR4)


Returns the length of each string in an array
var a1=["Dasfsdfas", "Dasffdfs", "DS", "2", "CCCCCC"]
var a2=a1.map (function (x) {return x.length})
Alert (A2)


This example is more commonly used, such as the use of Ajax grabbed a page, how to execute all the script block on the page?
var f=function (s) {return s.replace (/^<script.*?>/, ""). Replace (/<\/script>$/, "")}
O.value.match (/(?:<script.*?>) (\n|\r|.) *?) (?:<\/script>)/gi). Map (f). each (eval)

More complex examples
http://community.csdn.net/Expert/topic/4796/4796192.xml?temp=.2977564
</script>


<script>
Gets the first element that makes the function f true, if no return null is found
Array.prototype.find = function (f) {
for (Var i=0;i<this.length;i++) if (f (this[i])) return this[i]
return null
}

Gets all elements that make the function f true, if no return empty array is found
Array.prototype.findAll = function (f) {
var arr=[]
for (Var i=0;i<this.length;i++) if (f (this[i)) Arr.push (this[i))
Return arr
}

Find an even number in the array
var arr=[3,1,5,2,7,8,1,0,10]
var v=arr.find (function (x) {return x%2==0})
Alert (v)

Find all even
var arr2=arr.findall (function (x) {return x%2==0})
Alert (ARR2)

</script>

<script>
Returns an array that contains two elements. Both of these elements are an array arr1 is an array of all the elements that let F be true arr2 is the remaining element
Array.prototype.partition = function (f) {
var arr1=[],arr2=[]
for (Var i=0;i<this.length;i++) F (this[i])? Arr1.push (This[i): Arr2.push (this[i))
return [ARR1,ARR2]
}

Only all elements in an array can make F true, return true
Array.prototype.all = function (f) {
for (Var i=0;i<this.length;i++) if (!f (This[i)) return False
return True
}

As long as there is any element that can make F true, return true
Array.prototype.any = function (f) {
for (Var i=0;i<this.length;i++) if (f (this[i)) return True
return False
}

Look for even and odd numbers
var arr=[3,1,5,2,7,8,1,0,10]
var a=arr.partition (function (x) {return x%2==0})
Alert (a[0])//return all even
Alert (a[1])//All odd

var b=arr.all (function (x) {return x%2==0})//Check that all elements in an array are even
var c=arr.any (function (x) {return x%2==0})//Check if an even number exists in the array
Alert (b)
Alert (c)

</script>

Array.prototype.grep = function (regex,f)

To match each element of the array with the given regular expression,
Returns an array of matched elements, of course, if there is a function f, the result of the match is handled by the function f

if (! Array.prototype.push) {//As if ie5.0 the push () method is not supported below
Array.prototype.push=function () {
for (Var i=0;i<arguments.length;i++) {
This [this.length] = arguments[i];
}
return this.length;
}
}

var arr=[]
Arr.push ("1", "2", "3")


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.