Methods of arrays

Source: Internet
Author: User

Array.filter (function (Current,index,arr), thisvalue);

Function: Filter out an array that matches the criteria

Array.filter (Current,index,arr) {

Example: Var list=aqidata.filter (function (Item,index,array) {
return (ITEM[1]>=60);

Current: Required, the value of the currently-selected element

Index: optional, indexed value of the current element

Arr: Optional,

Thisvalue: Optional, the object is used as the execution callback, passed to the function, and used as the value of "this". If omitted, the value of this is undefined

}

Array.Sort (function (b) {

return a-B;

Array: return a[1]-b[1];

})

First, native JS ForEach () and map () traversal

Common:

1. All loops iterate through each item in the array.

Each execution of the anonymous function in 2.forEach () and map () supports 3 parameters: the current item in the array item, the index of the current item, and the original array input.

3. This in the anonymous function refers to window.

4. Only the array can be traversed.

1.forEach ()

no return value.

Arr[].foreach (function (Value,index,array) {

Do something

})

    • Parameter: The current item in the value array, index of the current item, array original array;
    • There are several items in the array, then the anonymous callback function passed in will need to be executed several times;
    • Theoretically, this method has no return value, simply iterates through each item in the array, does not modify the original array, but can modify the original array by itself by the index of the array;
    1. var ary = [12,23,24,42,1];
    2. var res = Ary.foreach (function (item,index,input) {
    3. Input[index] = item*10;
    4. })
    5. Console.log (RES);  //--> undefined;
    6. Console.log (ary); //--> changes the original array by the array index;


2.map ()

There is a return value, you can return it.

Arr[].map (function (Value,index,array) {

Do something

Return XXX

})

    • Parameter: The current item in the value array, index of the current item, array original array;
    • Difference: The return value is supported in the callback function of map, and what is returned is equivalent to what the item in the array is (it does not affect the original array, but it is equivalent to cloning a copy of the original array and changing the corresponding item in the array of the cloned copy);
    1. var ary = [12,23,24,42,1];
    2. var res = Ary.map (function (item,index,input) {
    3. return item*10;
    4. })
    5. Console.log (RES);  //-->[120,230,240,420,10]; A copy of the original array was copied and modified
    6. Console.log (ary); //-->[12,23,24,42,1]; The original array has not changed

Compatible wording:

Either foreach or map is incompatible under IE6-8 (incompatible cases do not have these two methods on Array.prototype), then we need to encapsulate a compatible method, the code is as follows:

  1. /**
  2. * foreach Traversal array
  3. * @param callback [function] callback function;
  4. * @param context [object] context;
  5. */
  6. Array.prototype.myForEach = function Myforeach (callback,context) {
  7. Context = Context | | Window
  8. if (' ForEach ' in array.prototye) {
  9. This.foreach (Callback,context);
  10. return;
  11. }
  12. //ie6-8 Write your own callback function execution Logic
  13. For (var i = 0,len = this.length; i < len;i++) {
  14. Callback && Callback.call (context,this[i],i, this);
  15. }
  16. }

  1. /**
  2. * Map Traversal array
  3. * @param callback [function] callback function;
  4. * @param context [object] context;
  5. */
  6. Array.prototype.myMap = function MyMap (callback,context) {
  7. Context = Context | | Window
  8. if (' map ' in array.prototye) {
  9. return This.map (callback,context);
  10. }
  11. //ie6-8 Write your own callback function execution Logic
  12. var newary = [];
  13. For (var i = 0,len = this.length; i < len;i++) {
  14. if (typeof callback = = = ' function ') {
  15. var val = callback.call (context,this[i],i, this);
  16. Newary[newary.length] = val;
  17. }
  18. }
  19. return newary;
  20. }

Ii. jQuery $.each () and $.map () traversal

Common:

You can iterate through the array and iterate over the object.

1.$.each ()

no return value. The anonymous function inside the $.each () supports 2 parameters: The current item's index I, the current item in the array, v. If the object is traversed, K is the key and V is the value.

$.each (arr, function (index,value) {

Do something

})

    • Parameters: Arr to iterate through the array, index the current item's indices, and the current item in the value array
    • The 1th and 2nd parameters are exactly the opposite of the above two functions, and be careful not to remember the wrong one.
    1. $.each (["a","B","C"], function (i, v) {
    2. Alert (i + ":" + V);
    3. });
    1. $ ("span"). each (function (i, v) {
    2. Alert (i + ":" + V);
    3. });

    1. $.each ({name: "John", Lang: "JS"}, function (k, v) {
    2. Alert ( "Name:" + K + ", Value:" + V);
    3. });

2.$.map ()

There is a return value, you can return it. The anonymous function inside $.map () supports 2 parameters and $.each () in the opposite position: the current item in the array V, the index of the current item. If the object is traversed, K is the key and V is the value. If it is $ ("span"). Map () Form, parameter Order and $.each () $ ("span"). each ().

$.map (arr, function (value, index) {

Do something

Return XXX

})

    1. var arr=$.map ([0,1,2], function (v) {
    2. return v + 4;
    3. });
    4. Console.log (arr);
    1. $.map ({"name":"Jim","age": +},function (k, v) {
    2. Console.log (k +":" +v);
    3. });


Methods of arrays

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.