Array

array.concat(item...);  //产生一个新数组
如果item,是一个数组,那么它的每个元素会被分别添加(浅复制,只解析一层)。
示例:
var a = [1, 3, 4];
var b = [5, 8];
var c = a.concat(b, true, false);
console.log(a);
console.log(b);
console.log(c);
=>1, 3, 4
=>5, 8
=>1, 3, 4, 5, 8, true, false

var a = [1, 3, 4];
var b = [[9, 9], 8];
var c = a.concat(b, true, false);
console.log(a);
console.log(b);
console.log(c);
=>1, 3, 4
=>[9, 9], 8
=>1, 3, 4, [9, 9], 8

array.join(separator);  //将数组中的元素,用separator连接成字符串。separator默认','。在IE6/7中效率优于+号连字符。
示例:
var a = [1, 3, true, 4];
console.log(a.join(''));
console.log(a.join('|'));
console.log(a);
=>13true4
=>1|3|true|4
=>1, 3, true, 4

array.slice(start, end);  //浅复制,[start, end)左闭右开。end默认是array.length,如果start或end为负数,array.length会尝试与其相加。
示例:
var a = [5, 3, 4, 8];
var b = a.slice(1, 3);
var c = a.slice(-2, -1);
var d = a.slice(-2);
console.log(a);
console.log(b);
console.log(c);
console.log(d);
=>5, 3, 4, 8
=>3, 4
=>4
=>4, 8

array.splice(start, deleteCount, item...);  //移除一个或多个元素,并用新的item替换它们。返回一个包含被移除元素的数组。
模拟实现:
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Array.method('splice', function (start, deleteCount) {
    var len = this.length;
    var insertCount = Math.max(arguments.length - 2, 0);
    var gap = insertCount - deleteCount;
    var newLen = this.length + gap;
    var delArr = [];
    var delCountTemp = deleteCount;
    var i = 0;
    var j = 0;
    var k;
    start = start || 0;
    if (start < 0) {
        start = start + len;
    }
    start = Math.max(Math.min(start, len), 0);
    while (delCountTemp > 0) {
        delArr[i] = this[start + i];
        i++;
        delCountTemp--;
    }
    k = len - 1 - (start + deleteCount - 1);  //原数组中需要移动位置的元素个数
    if (gap > 0) {
        while ( k > 0) {
            this[len - 1 + gap - j] = this[len - 1 - j];
            j++;
            k--;
        }
        console.log(newLen);
        this.length = newLen;
    } else if (gap < 0) {
        while (k > 0) {
            this[start + deleteCount - 1 + gap + j + 1] = this[start + deleteCount - 1 + j + 1];
            j++;
            k--;
        }
        this.length = newLen;
    }
    for (i = 0; i < insertCount; i++) {
        this[start + i] = arguments[i + 2];
    }
    return delArr;
});
示例:
var a = [1, 3, 4, 5];
var b = a.splice(1, 1, [22, 4], 'aa');
console.log(a);
console.log(b);
=>1, [22, 4], 'aa', 4, 5
=>3

array.push(item...);  //在数组尾部增加元素,会修改原始array,返回数组长度。当item是一个数组,会将其视为一个元素。
模拟实现:
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Array.method('push', function () {
    this.splice.apply(this, [this.length - 1, 0].concat(Array.prototype.slice.apply(arguments)));
    return this.length;
});
示例:
var a = [1, 3, 4];
var b = [5, 8];
var c = a.push(b, true, false);
console.log(a);
console.log(b);
console.log(c);
=>1, 3, 4, [5, 8], true, false
=>5, 8
=>6

array.pop();  //移除array中最后一个元素并返回该元素。
模拟实现:
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Array.method('pop', function () {
    return this.splice(this.length - 1, 1)[0];  //基于splice实现
});
示例:
var a = [1, 3, true, 4];
var b = a.pop();
console.log(a);
console.log(b);
=>1, 3, true
=>4

array.unshift(item...);  //在数组头部部增加元素,会修改原始array,返回数组长度。当item是一个数组,会将其视为一个元素。IE6中的返回值,永远是undefined。
模拟实现:
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Array.method('unshift', function () {
    this.splice.apply(this, [0, 0].concat(Array.prototype.slice.apply(arguments)));
    return this.length;
});
实例:
var a = [1, 3, 4];
var b = [5, 8];
var c = a.unshift(b, true, false);
console.log(a);
console.log(b);
console.log(c);
=>[5, 8], true, false, 1, 3, 4
=>5, 8
=>6

array.shift();  //移除数组中第一个元素并返回该元素。通常,shift比pop慢得多。
模拟实现:
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Array.method('shift', function () {
    return this.splice(0, 1)[0];  //基于splice实现
});
示例:
var a = [5, 3, 4];
var b = a.shift();
console.log(a);
console.log(b);
=>3, 4
=>5

array.reverse();  //反转数组,返回当前数组。
示例:
var a = [1, 3, 4];
var b = a.reverse();
console.log(a);
console.log(b);
=>4, 3, 1
=>4, 3, 1

array.sort(compareFn);  //默认排序的元素都是字符串。可以自定义比较函数替换默认的比较函数。如果两参数相等,返回0。如果第一个参数应该在前,返回负数。如果第二个参数应该在前,返回正数。
示例:
var a = [5, 6, '31', 'a', 'A', 'ade', 'aDe'];
var b = a.sort();  //按字符串排序
console.log(a);
console.log(b);
=>'31', 5, 6, 'A', 'a', 'aDe', 'ade'
=>'31', 5, 6, 'A', 'a', 'aDe', 'ade'

var a = [3, 5, 11, 8, 33];
var b = a.sort(function (a, b) {//对数字排序
    return a - b;
});
console.log(a);
console.log(b);
=>3, 5, 8, 11, 33
=>3, 5, 8, 11, 33

var a = [5, 6, '3', 'a', 'A', 'ade', 'aDe'];
var b = a.sort(function (a, b) {//对数字和字符串排序
    if (a === b) {
        return 0;
    } else if (typeof a === typeof b) {
        return a < b ? -1 : 1;
    } else {
        return typeof a < typeof b ? -1 : 1;
    }
});
console.log(a);
console.log(b);
=>5, 6, '3', 'A', 'a', 'aDe', 'ade'
=>5, 6, '3', 'A', 'a', 'aDe', 'ade'

var a = [
    {'first': 5, 'second': 'd'},
    {'first': 'dd', 'second': 44},
    {'first': 3, 'second': 'e2'},
    {'first': '3a', 'second': 3}
];
var by = function (name) {//对对象排序,sort方法是不稳定的(两个相等元素的相对位置不固定),因此链式多次调用该by方法,不能保证得到想要的结果。可以使用下面的增强版,来得到想要的结果。
    return function (a, b) {
        var temp1, temp2;
        if (typeof a === 'object' && typeof b === 'object' && a && b) {
            temp1 = a[name];
            temp2 = b[name];    
            if (temp1 === temp2) {
                return 0;
            } else if (typeof temp1 === typeof temp2) {
                return temp1 < temp2 ? -1 : 1;
            } else {
                return typeof temp1 < typeof temp2 ? -1 : 1;
            }
        } else {
            throw {
                'name': '排序出错!',
                'message': '数组含有Object类型的元素'
            };
        }
    };
};
var b = a.sort(by('first'));  
console.log(a);
console.log(b);
=>{'first': 3, 'second': 'e2'},    
  {'first': 5, 'second': 'd'},
  {'first': '3a', 'second': 3},
  {'first': 'dd', 'second': 44}    
=>{'first': 3, 'second': 'e2'},    
  {'first': 5, 'second': 'd'},
  {'first': '3a', 'second': 3},
  {'first': 'dd', 'second': 44}

var a = [
    {'first': 5, 'second': 'd'},
    {'first': 'dd', 'second': 44},
    {'first': 3, 'second': 'e2'},
    {'first': 3, 'second': 3}
];
var by = function (name, minor) {//minor是次级比较函数,在a和b相等时,调用。
    return function (a, b) {
        var temp1, temp2;
        if (typeof a === 'object' && typeof b === 'object' && a && b) {
            temp1 = a[name];
            temp2 = b[name];    
            if (temp1 === temp2) {
                return typeof minor === 'function' ? minor(a, b) : 0;
            } else if (typeof temp1 === typeof temp2) {
                return temp1 < temp2 ? -1 : 1;
            } else {
                return typeof temp1 < typeof temp2 ? -1 : 1;
            }
        } else {
            throw {
                'name': '排序出错!',
                'message': '数组含有Object类型的元素'
            };
        }
    };
};
var b = a.sort(by('first'));  
console.log(a);
console.log(b);
=>{'first': 3, 'second': 3},    
  {'first': 3, 'second': 'e2'},
  {'first': 5, 'second': 'd'},
  {'first': 'dd', 'second': 44}    
=>{'first': 3, 'second': 3},    
  {'first': 3, 'second': 'e2'},
  {'first': 5, 'second': 'd'},
  {'first': 'dd', 'second': 44}

2015-03-22——js常用的Array方法的更多相关文章

  1. js常用的array方法

      1. splice() splice()方法向/从数组中添加/删除项目,然后返回被删除的项目.(注释:该方法会改变原始数组.) arrayObject.splice(index,howmany,i ...

  2. JS常用时间处理方法

    这里会扩展一些JS常用时间处理方法,内置时间对象的方法不再赘述 -- 传送门:http://www.w3school.com.cn/js/jsref_obj_date.asp 时间格式化 -- 转换为 ...

  3. JS常用字符串处理方法应用总结

    这篇文章主要总结了JS常用字符串的处理方法,需要的朋友可以参考下   1.indexOf()方法,从前往后查找字符串位置,大小写敏感,从0开始计数.同理,lastIndexOf() 方法从后往前,两个 ...

  4. js常用几种类方法实现

    js定义类方法的常用几种定义 1 定义方法,方法中包含实现 function createCORSRequest() { var xhr = new XMLHttpRequest(); xhr.onl ...

  5. JavaScript基础 -- js常用内置方法和对象

    JS中常用的内置函数如下: 1.eval(str):计算表达式的结果. 2.parseInt(str,n):将符串转换成整数数字形式(可指定几进制). 3.parseFloat(str):将字符串转换 ...

  6. js常用字符串处理方法

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  7. js常用的数组方法

    1.创建数组的基本方法:  1.1 空数组  var obj=new Array();                 1.2 指定长度数组  var obj=new Array(size);     ...

  8. js 常用业务工具方法 (es5,es6)持续更新

    数组去重 数组去重最原始的方法就是使用双层循环. es5: // 使用indexOf function unique(array) { var res = []; for (var i = 0, le ...

  9. Js 常用调试的方法

    A  使用alert() 和document.write() 方法监视变量值 如果要中断代码的运行,监视变量的值,则使用alert() 方法: 如果需要查看的值很多,则使用document.write ...

随机推荐

  1. 反射学习2-通过反射机制动态获取属性的值模拟Struts的自动赋值

    一.准备知识:   Java反射机制   处理事务的JavaBean   String的操作常用方法 二.模拟步骤   这里我们通过反射机制动态获取属性的值模拟Struts中的自动赋值. 1.首先创建 ...

  2. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  3. docker动态添加磁盘

    docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter #!/bin/bash #This script is dynamic mount ...

  4. CCNA2.0笔记_Trunk&EtherChannel

    show interfaces trunk //查看Trunk信息 show interfaces fastEthernet 0/1 //查看接口二层信息 show interfaces fastEt ...

  5. Aspose.Cells 导出指定格式项目(金额、数字、文本)

    Aspose.Cells为开发人员提供了许多内嵌的数字和日期格式,开发人员可以通过Style对象的Number属性调用这些内嵌格式,下面是Aspose.Cells提供的显示格式:Value Type ...

  6. Android实例-IdHTTP下载(并实现自动安装)(XE10+小米2)

    相关资料: 1.群号 383675978 2.运行时提示"connection closed gracefully"错误原因与解决 http://www.delphifans.co ...

  7. 关于MongoDB最大连接数的查看与修改

    一. MongoDB连接数 在Linux平台下,无论是64位或者32位的MongoDB默认最大连接数都是819,WIN平台不知道,估计也没有人在 WIN平台下使用MongoDB做生产环境 [root@ ...

  8. php 知乎爬虫

    http://blog.jobbole.com/88788/ https://github.com/owner888/phpspider 费了半天劲安装了redis,导出cookie,发现仍是缺失很多 ...

  9. redux sample with slim redux source code

    code sample没有package.json文件,也就没有任何外部依赖,直接使用slim redux source code. slim resux只有90多行. nodejs对es6的impo ...

  10. CSS样式设置

    转载来自:http://www.imooc.com/article/2067水平居中设置-行内元素 水平居中 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align: ...