zoukankan      html  css  js  c++  java
  • 【原创】Array方法笔记

    1、sort(orderfunction):按指定的参数对数组进行排序,默认以字符串的字典顺序进行排序(如果是数字,会先转成字符),如果都是数字,则按数字第一位大小排序,如:

    var arr=[10,-2,4,1,8,15,8,49,27,6],b=arr.sort();console.log(b);
    
    [-2, 1, 10, 15, 27, 4, 49, 6, 8, 8]
    undefined
    

    添加orderfunction,可排序纯数字数组,如下:

    1 unction sortnumber(a,b){return a-b}
    2 
    3 var arr=[10,-2,4,1,8,15,8,49,27,6],
    4 
    5 b=arr.sort(sortnumber);console.log(b);
    6 
    7 [-2, 1, 4, 6, 8, 8, 10, 15, 27, 49]
    8 undefined

    sort()的返回值和原值都是排序过的值,检验如下:

     1 function sortnumber(a,b){return a-b}
     2 var arr=[10,-2,4,1,8,15,8,49,27,6],
     3 b=arr.sort(sortnumber);console.log(arr);
     4 
     5 [-2, 1, 4, 6, 8, 8, 10, 15, 27, 49]
     6 undefined
     7 
     8 
     9 function sortnumber(a,b){return a-b}
    10 var arr=[10,-2,4,1,8,15,8,49,27,6]
    11 ,b=arr.sort(sortnumber);console.log(arr==b);
    12 
    13 true
    14 undefined

    对对象数组进行排序,可以用以下函数:

    /by函数接受一个成员名字符串做为参数
    
    //并返回一个可以用来对包含该成员的对象数组进行排序的比较函数
    
    var by = function(name){
    
        return function(o, p){
            var a, b;
             if (typeof o === "object" && typeof p === "object" && o&& p) {
                a = o[name];
                b = p[name];
                if (a === b) {
                   return 0;
                }
                if (typeof a === typeof b) {
                    return a < b ? -1 : 1;
                }
               return typeof a < typeof b ? -1 : 1;
    
           }
           else {
                throw ("error");
            }
        }
    }

    调用by函数可以这样写:

    
    var employees=[]
    employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
    employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
    employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
    employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}
    
    直接调用函数:
    employees.sort(by("age"));

    2、slice(start,end)英文名为“切片” 表示从数组中切去某一片段,start为开始索引,end为终止索引位置(切出来的部分包括start,不包括end),原数组不变

     1 var arr=[10,-2,4,1,8,15,8,49,27,6],
     2 b=arr.slice(3,6);console.log(arr==b);
     3 
     4 false
     5 undefined
     6 
     7 var arr=[10,-2,4,1,8,15,8,49,27,6],
     8 b=arr.slice(3,6);console.log(b);
     9 
    10 [1, 8, 15]
    11 undefined
    12 
    13 var arr=[10,-2,4,1,8,15,8,49,27,6],
    14 b=arr.slice(3,6);console.log(arr);
    15 
    16 [10, -2, 4, 1, 8, 15, 8, 49, 27, 6]
    17 undefined

     另外,start=end时,切出来为空数组b= [];start为负数时,切出来也为空数组b=[];

    当start为正数时,end为负数时,-1表示数组表示最后一个(切片是end为最后一个,左闭右开,不切出来),-2表示倒数第二个,这时,取不到最后一个数字,以此类推,测试如下:

    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,3);console.log(b);
    
    []
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,-1);console.log(b);
    
    [1, 8, 15, 8, 49, 27]
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(-1,2);console.log(b);
    
    []
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(-1,5);console.log(b);
    
    []
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(-1,5);console.log(arr);
    
    [10, -2, 4, 1, 8, 15, 8, 49, 27, 6]
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,0);console.log(b);
    
    []
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,-2);console.log(b);
    
    [1, 8, 15, 8, 49]
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,-0);console.log(b);
    
    []
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,-1);console.log(b);
    
    [1, 8, 15, 8, 49, 27]
    undefined
    
    var arr=[10,-2,4,1,8,15,8,49,27,6],
    b=arr.slice(3,11);console.log(b);
    
    [1, 8, 15, 8, 49, 27, 6]
    undefined
  • 相关阅读:
    大数据处理系列之(二)系统过载保护
    大数据处理系列之(一)Java线程池使用
    js实现递归菜单无限层
    treeTable实现排序
    spring-dm 一个简单的实例
    Equinox OSGi服务器应用程序的配置步骤 (支持JSP页面)
    Spring DM 2.0 环境配置 解决Log4j问题
    Spring.DM web开发环境搭建
    STL容器的排序
    排序例子
  • 原文地址:https://www.cnblogs.com/pm-dongjian/p/5528833.html
Copyright © 2011-2022 走看看