1.toString:返回以数组种的每个值得字符串形式拼接而成得一个以逗号分割得字符串

  1. toStringArr = [1, 2, 3, 4, 5, 6]
  2. console.log(toStringArr.toString())
  3. // -> 1,2,3,4,5,6

2.valueOf: 返回数组对象的原始值。返回的还是数组

  1. valueOfArr = [1, 2, 3, 4, 5, 6]
  2. console.log(valueOfArr.valueOf())
  3. // -> [1, 2, 3, 4, 5, 6]

3.join: 通过指定的分隔符进行分隔并返回一个字符串

  1. var arr = [1, 2, 3, 4, 5, 6]
  2. var joinArr = arr.join('&')
  3. console.log('join:' + joinArr)
  4. // -> join:1&2&3&4&5&6

4.push: 向数组的末尾添加一个或更多元素,并返回新的长度

  1. var pushArr = [1, 2, 3, 4, 5, 6]
  2. pushArr.push(7)
  3. console.log(pushArr)
  4. // -> [1, 2, 3, 4, 5, 6, 7]

5.pop: 删除数组的最后一个元素并返回删除的元素, 如果数组为空就返回undefined

  1. var popArr = [1, 2, 3, 4, 5, 6]
  2. var popEle = popArr.pop()
  3. console.log(popEle, popArr)
  4. // -> 6, [1, 2, 3, 4, 5]

6.shift: 删除并返回数组的第一个元素, 如果数组为空,则shift() 方法不进行任何操作,返回undefined

  1. var shiftArr = [1, 2, 3, 4, 5, 6]
  2. var shiftEle = shiftArr.shift()
  3. console.log(shiftEle, shiftArr)
  4. // -> 1, [2, 3, 4, 5, 6]

7.unshift: 向数组的开头添加一个或更多元素,并返回新的长度

  1. var unshiftArr = [1, 2, 3, 4, 5, 6]
  2. unshiftArr.unshift(0)
  3. console.log(unshiftArr)
  4. // -> [0, 1, 2, 3, 4, 5, 6]

8.reverse: 反转数组的元素顺序

  1. var reverseArr = [1, 2, 3, 4, 5, 6]
  2. reverseArr.reverse()
  3. console.log(reverseArr)
  4. // -> [6, 5, 4, 3, 2, 1]

9.sort: 对数组的元素进行排序

  1. // a.
  2. var sortArr1 = [1, 3, 5, 2, 7, 6]
  3. sortArr1.sort()
  4. console.log(sortArr1)
  5. // -> [1, 2, 3, 5, 6, 7]
  6. // b. 因为sort排序是从左至右比较,只要其中一个比较出了结果,就直接返
  7. var sortArr2 = [1, 3, 19, 5, 17, 6]
  8. sortArr2.sort()
  9. console.log(sortArr2)
  10. // -> [1, 17, 19, 3, 5, 6]
  11. // c: 封装sort
  12. function sort(arr, type) {
  13. type = type || 1
  14. return arr.sort( (a, b) => {
  15. switch(type) {
  16. case 1: // 从小到大
  17. return a - b
  18. case 2: // 从大到小
  19. return b - a
  20. case 3: // 随机排序
  21. return Math.random() - 0.5
  22. default:
  23. return arr
  24. }
  25. })
  26. }
  27. var sortArr3 = [1, 3, 19, 5, 17, 6]
  28. sort(sortArr3)
  29. console.log(sortArr3)

10.concat:连接两个或更多的数组,并返回结果

  1. var concat1 = [1, 2, 3],
  2. concat2 = [4, 5, 6]
  3. concatArr = concat1.concat(concat2)
  4. console.log(concatArr)
  5. // -> [1, 2, 3, 4, 5, 6]

11.slice(start, end): 选取数组的一部分,并返回一个新数组, start必须,end可选

  1. var sliceArr = [1, 2, 3, 4, 5, 6]
  2. var sliceNewArr = sliceArr.slice(1, -1) // 截取第二个 到 倒数第二个
  3. console.log(sliceNewArr)
  4. // -> [2, 3, 4, 5]

12.splice(index, howmany, item1,.....,itemX): 从数组中添加或删除元素

  1. /*
  2. index: 必需。规定从何处添加/删除元素。 该参数是开始插入和(或)删除的数组元素的下标,必须是数字
  3. howmany: 必需。规定应该删除多少元素。必须是数字,但可以是 "0"。 如果未规定此参数,则删除从 index 开始到原数组结尾的所有元素。
  4. item1,.....,itemX: 可选。要添加到数组的新元素
  5. */
  6. var spliceArr1 = [1, 2, 3, 4, 5, 6]
  7. spliceArr1.splice(2, 2, 10, 12) // 从下标为2 的地方开始删除 后面的两个元素, 并在这个地方插入 10,12两个元素
  8. console.log(spliceArr1)
  9. // -> [1, 2, 10, 12, 5, 6]
  10.  
  11. var spliceArr = [1, 2, 3, 4, 5, 6]
  12. spliceArr.splice(2, 0, 10, 12) // 当需要删除的元素为0, 相当于在这个位置插入元素
  13. console.log(spliceArr)
  14. // -> [1, 2, 10, 12, 3, 4, 5, 6]

13.copyWithin(target, start, end): 从数组的指定位置拷贝元素到数组的另一个指定位置中

  1. /*
  2. target: 必需。复制到指定目标索引位置。
  3. start: 必需。元素复制的起始位置。
  4. end: 可选。停止复制的索引位置 (默认为 array.length)
  5. */
  6. var copyWithinArr = [1, 2, 3, 4, 5, 6]
  7. copyWithinArr.copyWithin(1, 2, 4)
  8. console.log(copyWithinArr)
  9. // -> [1, 3, 4, 4, 5, 6]

14.fill(value, start, end): 用于将一个固定值替换数组的元素

  1. /*
  2. value: 必需。填充的值。
  3. start: 可选。开始填充位置。
  4. end: 可选。停止填充位置 (默认为 array.length)
  5. */
  6. var fillArr = [1, 2, 3, 4, 5, 6]
  7. fillArr.fill(9, 2, 4)
  8. console.log(fillArr)
  9. // -> [1, 2, 9, 9, 5, 6]

15.includes(searchElement, fromIndex):用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false

  1. var includesArr = [1, 2, 3, 4, 5, 6]
  2. var includes1 = includesArr.includes(3)
  3. var includes2 = includesArr.includes(10)
  4. console.log(includes1, includes2)
  5. // -> true, false

16.indexOf(item,start):可返回某个指定的字符串值在字符串中首次出现的位置

  1. var indexOfArr = [1, 2, 3, 4, 3, 6]
  2. var indexOfEle = indexOfArr.indexOf(3)
  3. console.log(indexOfEle)
  4. // -> 2

17.lastIndexOf(item,start):返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索

  1. var lastIndexOfArr = [1, 2, 3, 4, 3, 6]
  2. var lastIndexOfEle = lastIndexOfArr.lastIndexOf(3)
  3. console.log(lastIndexOfEle)
  4. // -> 4

18.find(function(currentValue, index, arr),thisValue): 返回传入一个测试条件(函数)符合条件的数组第一个元素

  1. // function(currentValue, index, arr):
  2. // a. currentValue: 必需。当前元素
  3. // b. index: 可选。当前元素的索引值
  4. // c. arr: 可选。当前元素所属的数组对象
  5. // thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  6.  
  7. var findArr = [1, 2, 3, 4, 5, 6]
  8. var findEle = findArr.find( function(currentValue, index, arr) {
  9. return currentValue >= 4
  10. })
  11. console.log(findEle)

19.findIndex(function(currentValue, index, arr),thisValue): 返回符合测试条件的第一个数组元素索引,如果没有符合条件的则返回 -1

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  7.  
  8. */
  9. var findIndexArr = [1, 2, 3, 4, 5, 6]
  10. var findIndexEle = findIndexArr.findIndex( function(currentValue, index, arr) {
  11. return currentValue >= 4
  12. })
  13. console.log(findIndexEle)

20.forEach(function(currentValue, index, arr), thisValue): 用于调用数组的每个元素,并将元素传递给回调函数。没有返回值

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  7.  
  8. */
  9. var forEachArr = [1, 2, 3, 4, 5, 6]
  10. forEachArr.forEach( function(currentValue, index, arr){
  11. console.log(currentValue)
  12. })
  13. // -> 依次打印 1 2 3 4 5 6, 没有返回值

21.map(function(currentValue, index, arr), thisValue): 返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  7.  
  8. */
  9. var mapArr = [1, 2, 3, 4, 5, 6]
  10. var mapNewArr = mapArr.map( function(currentValue, index, arr){
  11. // console.log(currentValue)
  12. return currentValue*2
  13. })
  14. console.log(mapNewArr)
  15. // -> [2, 4, 6, 8, 10, 12]

22.reduce(function(total, currentValue, index, arr), thisValue): 接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. d: total: 必需。初始值, 或者计算结束后的返回值。
  7. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  8.  
  9. */
  10. var reduceArr = [1, 2, 3, 4, 5, 6]
  11. var reduceNewArr = reduceArr.reduce( function(total, currentValue, index, arr){
  12. return total - currentValue
  13. })
  14. console.log(reduceNewArr)
  15. // -> 19

23.reduceRight(function(total, currentValue, index, arr), thisValue): 接收一个函数作为累加器,数组中的每个值(从右到左)开始缩减,最终计算为一个值

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. d: total: 必需。初始值, 或者计算结束后的返回值。
  7. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  8.  
  9. */
  10. var reduceRightArr = [1, 2, 3, 4, 5, 6]
  11. var reduceRightNewArr = reduceRightArr.reduceRight( function(total, currentValue, index, arr){
  12. return total - currentValue
  13. })
  14. console.log(reduceRightNewArr)
  15. // -> -9

24.some(function(currentValue, index, arr), thisValue): 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。如果没有满足条件的元素,则返回false

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  7.  
  8. */
  9. var someArr = [1, 2, 3, 4, 5, 6]
  10. var someEle1 = someArr.some( function(currentValue, index, arr){
  11. return currentValue > 4
  12. })
  13. var someEle2 = someArr.some( function(currentValue, index, arr){
  14. return currentValue > 6
  15. })
  16. console.log(someEle1, someEle2)
  17. // -> true, false

25.every(function(currentValue, index, arr), thisValue): 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。如果所有元素都满足条件,则返回 true

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  7.  
  8. */
  9. var everyArr = [1, 2, 3, 4, 5, 6]
  10. var everyEle1 = everyArr.every( function(currentValue, index, arr){
  11. return currentValue > 0
  12. })
  13. var everyEle2 = everyArr.every( function(currentValue, index, arr){
  14. return currentValue > 1
  15. })
  16. console.log(everyEle1, everyEle2)
  17. // -> true, false

26.filter(function(currentValue, index, arr), thisValue):创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素

  1. /*
  2. function(currentValue, index, arr):
  3. a. currentValue: 必需。当前元素
  4. b. index: 可选。当前元素的索引值
  5. c. arr: 可选。当前元素所属的数组对象
  6. thisValue: 可选。 传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值
  7.  
  8. */
  9. var filterArr = [1, 2, 3, 4, 5, 6]
  10. var filterNewArr = filterArr.filter( function(currentValue, index, arr){
  11. return currentValue > 2
  12. })
  13. console.log(filterNewArr)
  14. // -> [3, 4, 5, 6]

27.from: 将类数组对象和可遍历对象转化为数组

  1. fromObj = {
  2. 0: '0',
  3. 1: '1',
  4. 3: '3',
  5. length:4
  6. }
  7. arrayArr = Array.from(fromObj)
  8. console.log(arrayArr)
  9. // -> ["0", "1", undefined, "3"]

28.of: 创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型

  1. Array.of(7); // -> [7]
  2. Array.of(1, 2, 3); // -> [1, 2, 3]
  3.  
  4. Array(7); // -> [ , , , , , , ]
  5. Array(1, 2, 3); // -> [1, 2, 3]

javascript 方法总结(Array篇)的更多相关文章

  1. JavaScript引用类型之Array数组的排序方法

    数组中已经存在两个JavaScript给我们定义好的重排序的方法:reverse()和sort()方法,下面来简单分析下: 1.reverse()    用于反转数组项的顺序,代码如下: <sc ...

  2. JavaScript中数组Array方法详解

    ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...

  3. JavaScript中的Array.prototype.slice.call()方法学习

    JavaScript中的Array.prototype.slice.call(arguments)能将有length属性的对象转换为数组(特别注意: 这个对象一定要有length属性). 但有一个例外 ...

  4. javascript中的array对象属性及方法

    Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, e ...

  5. JavaScript引用类型之Array数组的栈方法与队列方法

    一.栈方法 ECMAScript数组也提供了一种让数组的行为类似与其他数据结构的方法.具体的来说,数组可以变现的向栈一样,栈就是一种可以限制插入和删除向的数据结构.栈是一种LIFO(Last In F ...

  6. JavaScript 面向对象(一) —— 基础篇

    学好JS的面向对象,能很大程度上提高代码的重用率,像jQuery,easyui等,这篇博客主要从细节上一步步讲JS中如何有效地创建对象,也可以看到常见的创建对象的方式,最后也会附上一些JS面向对象的案 ...

  7. 曲线参数化的Javascript实现(代码篇)

    在曲线参数化的Javascript实现(理论篇)中推出了曲线弧长积分的公式,以及用二分法通过弧长s来查找样条曲线上对应的u,再求Q(u)的值.弧长积分函数如下: ,其中-----公式1 Simpson ...

  8. 深入理解javascript函数系列第二篇——函数参数

    × 目录 [1]arguments [2]内部属性 [3]函数重载[4]参数传递 前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传 ...

  9. javascript类型系统之Array

    原文:javascript类型系统之Array 目录 [1]数组创建 [2]数组操作 [3]继承的方法 [4]实例方法 数组转换 数组检测 栈和队列 排序方法 操作方法 位置方法 前面的话 数组是一组 ...

  10. JavaScript 面向对象(三) —— 高级篇

    JavaScript 面向对象(一) —— 基础篇 JavaScript 面向对象(二) —— 案例篇 一.json方式的面向对象 首先要知道,js中出现的东西都能够放到json中.关于json数据格 ...

随机推荐

  1. docker 网络实践

    #docker 网络模式 环境 centos7. , Docker version -ce docker自带网络类型 bridge,host,none,container,overlay,macvla ...

  2. flask之信号

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. pip3 install blinker 1. 内置信号 request_started = ...

  3. Flask从入门到精通之模型定义

    模型这个术语表示程序使用的持久化实体.在ORM 中,模型一般是一个Python 类,类中的属性对应数据库表中的列. Flask-SQLAlchemy 创建的数据库实例为模型提供了一个基类以及一系列辅助 ...

  4. JavaScript函数学习总结(一)---函数定义

    博客原文地址:Claiyre的个人博客 如需转载,请在文章开头注明原文地址 在许多传统的OO语言中,对象可以包含数据,还可拥有方法,也就是属于该对象的函数.但在JavaScript中,函数也被认为是一 ...

  5. windows系统正常但linux系统下找不到文件的问题

    问题描述:最近一个项目开发环境是windows系统,正式环境是linux系统.验证码图片在开发环境正常,但是在正式环境下一直报404. 问题原因:File dir = new File(realPat ...

  6. Android Ble4.0开发

    最近在做一个蓝牙的项目,是Blu4.0,Android系统支持是4.3版本以上(为此特地卖了个手机,). 当然也参考了不少大牛的的微博,说的都不错,再次特地感谢 http://blog.csdn.ne ...

  7. jquery ajax在跨域访问post请求的时候,包括ie9以下的浏览器无效,其他浏览器正常

    最近做的一个项目,原先没有要求兼容IE9以下,所以在写代码的时候也没有打开IE9以下去测试,这两天要去做IE9以下的兼容,在IE9以下打开本地的项目发现数据请求不成功,而通过发布道服务器上的链接打开是 ...

  8. git命令上传项目到码云总结

    码云上传项目git命令总结: git clone https://git.oschina.net/xh-lxx/xh-lxx.oschina.io.git 进入到克隆下来的文件夹,然后操作git命令 ...

  9. 使用BeanUitls提高对象拷贝效率

    首先来创建两个bean 注:一定要有set/get方法,成员变量必须要同名 public class User1 { String name; String password; String phon ...

  10. POJ 2301

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...