JavaScript之Array常用函数汇总
[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常用函数汇总的更多相关文章
- 非常实用的PHP常用函数汇总
这篇文章主要介绍了非常实用的PHP常用函数,汇总了加密解密.字符串操作.文件操作.SQL注入等函数的实例与用法说明,在PHP项目开发中非常具有实用价值,需要的朋友可以参考下 本文实例总结了一些在php ...
- 【PHP】最详细PHP从入门到精通(三)——PHP中的数组常用函数汇总
PHP从入门到精通 之PHP中的数组常用函数详解 数组作为PHP中最常用的结构之一,PHP强大的数组函数功能,给数组的相关操作带来了极大的便利.今天给大家介绍的PHP中数组函数,是PHP数组中重要的 ...
- php常用函数汇总
php常用函数汇总 字符串截取: 1.substr('要截取的字符串','从第几个字符开始','到第几个字符结束'); * 截取英文或者数字 ...
- 思迈特软件Smartbi:Excel数据分析常用函数汇总!
多传统行业的数据分析师只要求掌握Excel即可,会SPSS/SAS是加分项.即使在挖掘满街走,Python不如狗的互联网数据分析界,Excel也是不可替代的. Excel是我们工作中经常使用的一种工具 ...
- OpenCV图像处理中常用函数汇总(1)
//俗话说:好记性不如烂笔头 //用到opencv 中的函数时往往会一时记不起这个函数的具体参数怎么设置,故在此将常用函数做一汇总: Mat srcImage = imread("C:/Us ...
- TensorFlow 常用函数汇总
本文介绍了tensorflow的常用函数,源自网上整理. TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU ...
- mysql常用函数汇总(分享)
以下是对mysql中的常用函数进行了汇总介绍.需要的朋友可以过来参考下. 一.数学函数ABS(x) 返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x ...
- Mysql常用函数汇总-经典实用
以下是对mysql中的常用函数进行了汇总介绍.需要的朋友可以过来参考下. 一.数学函数ABS(x) 返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x ...
- Mysql 常用函数(1)- 常用函数汇总
Mysql常用函数的汇总,可看下面系列文章 Mysql常用函数有哪几类 数值型函数 字符串型函数 日期时间函数 聚合函数 流程控制函数 数值型函数 函数名称 作用 ABS 求绝对值 SQRT 求二次方 ...
随机推荐
- python Request库
命令行查看版本:python --version pip --version pip常用命令// 安装包pip install xxx// 升级包pip install -U xxx// 卸载包pip ...
- JDK环境变量的配置方法
1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 变量名:JAVA_HOME 变量值:jdk的目录,比如d:/java 3. 选择“系统变量”中变量名为“Path”的环 ...
- WORD2003电子签名插件(支持手写、签章)
1.引言 WORD电子签名插件,支持手写.本地电子图章.以及网络图章功能.软件使用VC6,以ATL方式编写,软件小巧精致. 这是我学习ATL的成果,学习过程及程序的编写,前前后后共用了一个多月的时间, ...
- 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
转:http://blog.csdn.net/iunion/article/details/12185077 之前用Text Kit写Reader的时候,在分页时要计算一段文本的尺寸大小,之前使用 ...
- django开发的社区和博客
社区 线上地址:http://codetheme.sinaapp.com/ Githubhttps://github.com/BeginMan/codetheme 由于利用两周下班时间熬夜做的,难免有 ...
- Selenium 3 -how to locate the chromedriver and geckodriver place?
Maybe you met these exceptions sometimes: 1. Chrome Driver The path to the driver executable must be ...
- 测试markdown发布
- 推荐--《Android深入浅出》
基本信息 书名:Android深入浅出 作者:张旸 著 页数: 661 出版社: 机械工业出版社; 第1版 (2014年4月17日) 语种: 简体中文 ASIN: B00JR3P8X0 品牌: 北京华 ...
- C# 个人常用代码积累
/// <summary> /// TextBox限制只能输入十六进制,且只能输入6个 /// </summary> /// <param name="send ...
- ecshop 加广告出现广告位的宽度值必须在1到1024之间
打开 admin/ad_position.php这个文件,搜索1024,这里你会搜到两个地方 在236行左右 if ($ad_width > 1024 || $ad_width < 1) ...