[20141121]JavaScript之Array常用功能汇总

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

导语:在JavaScript中,Array是一个使用比较频繁的对象,那么它到底有哪些常用的方法呢?

首先,我们先看一下Array对象的类型:

typeof Array // 'function'
Array instanceof Object // true

从上可以看出,Array本质是一个function,同样派生自Object,定义如下:

function Array(args) {}

接下来,我们来看Array自身的方法:

#1、concat()

定义:原型方法,连接两个或更多的数组,并返回结果(新数组)。

Array.prototype.concat = function(items) {};

示例:

var arr1 = [1, 2];
var arr2 = arr1.concat([3, 4]);
var arr3 = arr2.concat([5, 6], [7, 8] ,10, {});
console.log(arr1); // [1, 2]
console.log(arr2); // [1, 2, 3, 4]
console.log(arr3); // [1, 2, 3, 4, 5, 6, 7, 8, 10, Object]

注意:concat不仅可以连接单个对象,也可以连接多个对象,同时如果是参数为数组,那么会将数组元素拆分并连接,如果是对象,则直接将对象连接。该方法不会改变原始数组

#2、join()

定义:原型方法,把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。

Array.prototype.join = function(separator) {};

示例:

var arr = [1, 2, 3];
console.log(arr.join('|')); // '1|2|3'
console.log(arr.join('')); // '123'
console.log(arr.join('---')); // '1---2---3'

注意:太常用了,没什么可注意的~

#3、pop()

定义:原型方法,删除并返回数组的最后一个元素。

Array.prototype.pop = function() {};

示例:

var arr1 = [1, 2, 3, 4];
var lastOne = arr1.pop();
console.log(lastOne); // 4
console.log(arr1); // [1, 2, 3]

注意:该方法无参数,有返回值,返回数组最后一个元素。该方法会改变原始数组

#4、push()

定义:原型方法,向数组的末尾添加一个或更多元素,并返回新的长度。

Array.prototype.push = function(items) {};

示例:

var arr1 = [1, 2];
var len = arr1.push(3);
var arr2 = arr1.push(4, 5);
console.log(len);
console.log(arr1);
console.log(arr2);

注意:该方法的返回值会返回数组的新长度。该方法会改变原始数组

#5、reverse()

定义:原型方法,颠倒数组中元素的顺序。

Array.prototype.reverse = function() {};

示例:

var arr1 = [1, 2, 3, 4, 5];
var res = arr1.reverse();
console.log(res);
console.log(arr1);

注意:该方法的返回值为自身(翻转后的值),该方法会改变原始数组

6、shift()

定义:原型方法,删除并返回数组的第一个元素。

Array.prototype.shift = function() {};

示例:

var arr1 = [1, 2, 3];
var res = arr1.shift();
console.log(res);
console.log(arr1);

注意:该方法返回数组第一个元素,和pop()方法对应(返回并删除最后一个元素)。该方法会改变原始数组

#7、slice()

定义:原型方法,从某个已有的数组返回选定的元素。

Array.prototype.slice = function(start,end) {};

示例:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res1 = arr.slice(0, 3);
var res2 = arr.slice(0, 100);
var res3 = arr.slice(-1,-6);
var res4 = arr.slice(-6, -1);
console.log(res1);
console.log(res2);
console.log(res3);
console.log(res4);
console.log(arr)

注意:该方法支持逆向索引,同时索引采取区间左闭右开的原则。该方法不会改变原始数组

#8、sort()

定义:原型方法,对数组的元素进行排序。

Array.prototype.sort = function(compareFn) {};

示例:

var arr = [1, 5, 2, 3, 4, 7, 8, 6, 9];
var res1 = arr.sort(); //如果是数字,默认从小到大排序
console.log(res1);
var arr2 = ['a', 'c', 'b'];
var res2 = arr2.sort();//如果是字符,按照字符顺序(ASCII,字符串同)排序
console.log(res2);
//遇到复杂数据,经过测试是按照数组<正则<数字<对象<字符串<函数 这个顺序
var arr3 = [{name:'name'}, 134, 'aaa', function(){}, [], /a/];
var res3 = arr3.sort();
console.log(arr3); //可以通过自定义规则实现复杂的排序
var res4 = arr.sort(function(a1, a2){
if(a1 === a2){ // 两者相等,那么就算想等
return 0;
}
if(a1%3 === 0){ //如果a1被3整除,那么a1小
return -1;
}
if(a2%3 === 0){ //如果a2被3整除,那么a2小
return 1;
}
return a2%3-a2%3; //不满足以上条件,那么根据余数比大小,余数小的元素小
})
console.log(res4);

注意:该方法返回自身(排序后数组)。可通过function(a1, a2){}实现非常复杂的排序规则。该方法会改变原始数组

#9、splice()

定义:原型方法,删除元素,并向数组添加新元素。(该方法相等较复杂,悠着点用)

Array.prototype.splice = function(start,deleteCount,items) {};

示例:

var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res1 = arr1.splice(0, 3, 'new1', 'new2');
console.log(res1); // [1, 2, 3]
console.log(arr1); // ['new1', 'new2', 4, 5, 6, 7, 8, 9] arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
res1 = arr1.splice(-6, 3, 'new1', 'new2');
console.log(res1); // [4, 5, 6]
console.log(arr1); // [1, 2, 3, 'new1', 'new2', 7, 8, 9]

注意:splice()函数支持倒叙索引,同时第二个参数是长度(不是下标),新插入的数据会插入在start下标位置。返回值为删除的元素数组。该方法会改变原始数组

#10、unshift()

定义:原型方法,向数组的开头添加一个或更多元素,并返回新的长度。

Array.prototype.unshift = function(items) {};

示例:

var arr1= [1, 2, 3];
var res1 = arr1.unshift('new1', 'new2');
console.log(res1); // 5
console.log(arr1); // ["new1", "new2", 1, 2, 3]

注意:该方法和push相对(在末尾添加元素,返回新长度),该方法的返回值是新数组长度。该方法会改变原始数组

我们还可以为Array添加更多的常用功能,比如:

Array.prototype.where = function(predicateFn){
var parameterIsFn = typeof predicateFn === 'function'
var result = [];
for(var i = 0, len = this.length; i < len; i++){
if(!parameterIsFn || predicateFn(this[i])){
result.push(this[i]);
}
}
return result;
}; var arr = ['new1', 'new2', 1, 2, 3];
var res = arr.where(function(item){
return typeof item === 'number';
});
console.log(res);

JavaScript之Array常用函数汇总的更多相关文章

  1. 非常实用的PHP常用函数汇总

    这篇文章主要介绍了非常实用的PHP常用函数,汇总了加密解密.字符串操作.文件操作.SQL注入等函数的实例与用法说明,在PHP项目开发中非常具有实用价值,需要的朋友可以参考下 本文实例总结了一些在php ...

  2. 【PHP】最详细PHP从入门到精通(三)——PHP中的数组常用函数汇总

     PHP从入门到精通 之PHP中的数组常用函数详解 数组作为PHP中最常用的结构之一,PHP强大的数组函数功能,给数组的相关操作带来了极大的便利.今天给大家介绍的PHP中数组函数,是PHP数组中重要的 ...

  3. php常用函数汇总

    php常用函数汇总   字符串截取:           1.substr('要截取的字符串','从第几个字符开始','到第几个字符结束');             * 截取英文或者数字       ...

  4. 思迈特软件Smartbi:Excel数据分析常用函数汇总!

    多传统行业的数据分析师只要求掌握Excel即可,会SPSS/SAS是加分项.即使在挖掘满街走,Python不如狗的互联网数据分析界,Excel也是不可替代的. Excel是我们工作中经常使用的一种工具 ...

  5. OpenCV图像处理中常用函数汇总(1)

    //俗话说:好记性不如烂笔头 //用到opencv 中的函数时往往会一时记不起这个函数的具体参数怎么设置,故在此将常用函数做一汇总: Mat srcImage = imread("C:/Us ...

  6. TensorFlow 常用函数汇总

    本文介绍了tensorflow的常用函数,源自网上整理. TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU ...

  7. mysql常用函数汇总(分享)

    以下是对mysql中的常用函数进行了汇总介绍.需要的朋友可以过来参考下. 一.数学函数ABS(x) 返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x ...

  8. Mysql常用函数汇总-经典实用

    以下是对mysql中的常用函数进行了汇总介绍.需要的朋友可以过来参考下. 一.数学函数ABS(x) 返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x ...

  9. Mysql 常用函数(1)- 常用函数汇总

    Mysql常用函数的汇总,可看下面系列文章 Mysql常用函数有哪几类 数值型函数 字符串型函数 日期时间函数 聚合函数 流程控制函数 数值型函数 函数名称 作用 ABS 求绝对值 SQRT 求二次方 ...

随机推荐

  1. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  2. jQuery 之父:每天写代码

    去年秋天我的支线代码项目 遇到了一些问题,项目进展不足,而且我没法找到一个完成更多代码的方法(在不影响我在Khan Academy方面的工作的前提下). 我主要在周末进行我的支线,当然有时候也在晚上进 ...

  3. nginx是一个反向代理的软件

    nginx只是一个反向代理的软件,和语言无关,理论上支持任何Web平台,当然http://Asp.net也不例外,http://51aspx.com就是http://Asp.net开发的,前端暴漏的是 ...

  4. libuv在cocos2d-x中的使用

    libuv经过Node.js的实践和应用,已经证明非常之成熟,本来之前项目用的是这个:clsocket https://github.com/DFHack/clsocket  当初选它的主要原因是它支 ...

  5. 刷连记录的迟到检测---Table表格增加一列值

    公司OA新增加了 刷脸记录 ,用于查看自己是否迟到,但是没有什么提醒,于是乎自己写了一个脚本 刷连记录 类似于这样的: 运行脚本后,是这个样子的: 擦,我本月已经迟到了 3次了.... 拖拽 刷脸记录 ...

  6. 从WEB SERVICE 上返回大数据量的DATASET

    前段时间在做一个项目的时候,遇到了要通过WEB SERVICE从服务器上返回数据量比较大的DATASET,当然,除了显示在页面上以外,有可能还要用这些数据在客户端进行其它操作.查遍了网站的文章,问了一 ...

  7. JavaScript 中数组实用浅析

    本文适用于HTML.ASP 中的 JavaScript 脚本代码.代码以 HTML 中的 JS 为例,如果在 ASP 中,请将 document.write 改为 Response.Write 即可. ...

  8. Swift入门篇-结构体

    前面主要是介绍swift语言中基本类型的用法,今天给大家介绍的是swift的结构体的用法,swift中结构体的用法和其他语言的用法,还有不太一样,不过您多敲几遍,就可以理解结构体,结构体在ios开发中 ...

  9. android手机两种方式获取IP地址

    1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...

  10. [转] 配置Log4j

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...