Prototype extension of the JavaScript array Traversal map ()

Source: Internet
Author: User
Tags javascript array

In JavaScript 1.6, JavaScript arrays add several useful methods: IndexOf, LastIndexOf, every, filter, ForEach, map, some, where the first two can be attributed to the element location method, Some of the latter can be categorized as iterative (iterative) methods. Chibi Senior Middle School

Unfortunately, these new methods are not supported by all browsers, in which case we need to do it ourselves, and in these introductory articles we also provide implementations in browsers that do not support these new features.

The native methods are as follows:

var Mappedarray = Array.map (callback[, Thisobject]);
    • Callback: The callback function to execute for each array element.
    • Thisobject: The This object that is defined when the callback function is executed.

Each element in an array executes one specified function (callback) at a time and creates a new array for the element with the result of each return. It executes the specified function only for non-empty elements in the array, no assignment, or the deleted element is ignored.

The callback function can have three parameters: the current element, the index of the current element, and the current array object. If the parameter thisobject is passed in, it will be treated as the this object inside the callback function (callback), and if it is not passed or null, the global object will be used.

Map does not change the original array, remember: Only the array elements passed in before the callback function executes, the elements that are added after the callback function begins execution are ignored, and when the callback function begins execution to the last element, the array element is deleted or changed, whichever time the callback function accesses the element. , the deleted element is ignored.

If the browser does not support the map method, you can also use prototype to expand it as follows:

<script type= "text/javascript" >//extension prototype Object Array.prototype.map = function (fn) {    var a = [];    for (var i = 0; i < this.length; i++) {        var value = fn (this[i], i);        if (value = = null) {            continue;//If the function FN returns NULL, the item is removed from the array        }        A.push (value);    }    Return a;};/ /example, arr is the original array var arr = [    {name: ' Gonn ', age:20, Sex: ' 1 ', No: ' 274200 '},    {name: ' Nowamagic ', age:30, Sex: ' 0 ', N o: ' 274011 '},    {name: ' Frie ', age:40, Sex: ' 1 ', No: ' 274212 '}    ];//use map to change the value of each item in the array, either by adding or removing the elements within each element of the group, or by return Null to remove an item of the array var arr2 = Arr.map (function (item, i) {    Item.sex = Item.sex = = ' 0 '? ' Female ': ' Male ';    if (Item.name = = ' Tom ') {        return null;//delete item with name Tom    }    return {        index:i,        name:item.name,        Age:item.age + + i,        sex:item.sex    };}); Console.log (ARR2);</script>

In the Firefox firebug console output:

[Object {index=0, name= "Gonn", age=50, More ...}, object {index=1, name= "Nowamagic", age=61, More ...}, object {index=2, name = "Frie", age=72, More ...}]

Or, you can extend it in the following ways:

if (! Array.prototype.map) {    Array.prototype.map = function (Fun/*, thisp*/)    {        var len = this.length;        if (typeof fun! = "function")            throw new TypeError ();         var res = new Array (len);        var thisp = arguments[1];        for (var i = 0; i < len; i++)        {            if (I-in-)                res[i] = Fun.call (Thisp, this[i], I, this);        }         return res;    };}

Happy Coding.

Prototype extension of the JavaScript array Traversal map ()

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.