数组的常用方法

数组(Array)
创建数组 : var arr = new Array( ) 通过构造函数创造数组
var arr = [ ] 通过字面量创造数组
变量课访问的范围 是作用域
全局作用域 是函数之外的执行环境(程序的任何地方都可以使用)
局部作用域 只能在本函数中使用
数组名.unshift(要添加的项,多个) 在数组开始位置添加
数组名.push(添加的项,可多个) 在数组结束位置添加(常用)
数组名.shift() 删除并返回数组的第一个元素, 删除第一个
数组名.pop() 删除并返回数组的最后一个元素, 删除最后一个
数组名.splice(索引,删除的个数,替换项) 从数组中添加、删除任意位置
数组.splice(index,howmany,item)
参数:  index 从哪个位置开始删,
    howmany 删除几个 
    item新添加的数据(可以是多个) 可选值
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
// 删除 var arr1 = arr.splice(2, 2) // 可以返回一个的新数组 console.log(arr1); // [3, 4] 截取的结果 console.log(arr); //  [1, 2, 5, 6, 7, 8, 9]  // 如果截取的个数是 0 意思就是 从这个位置添加 var arr2 = arr.splice(2, 0, 'a', 'b') console.log(arr2); // [] 没有删除 console.log(arr); // [1, 2, "a", "b", 5, 6, 7, 8, 9]
数组名.indexOf('要查询的东西') 从前向后查询|不存在返回-1
数组名.lastIndexOf('要查询的东西') 从后向前查询|不存在放回-1
数组名.reverse()  颠倒数组中元素顺序
 let arr8 = [1,2,3,4,5,6]
 let arr9 = arr8.reverse()
 console.log(arr8,arr9) // [6, 5, 4, 3, 2, 1]  [6, 5, 4, 3, 2, 1]
sort也可以排列字符串的顺序
var arr = ['v','s','d','f','h','r','t','u','z','a','e','i','o','p','k']
arr.sort()
console.log(arr); // ["a", "d", "e", "f", "h", "i", "k", "o", "p", "r", "s", "t", "u", "v", "z"]// 注意 如果要排列数字的会 最好不要直接使用 sort 以为他值可以 排列单数 如下
var arr = [1,2,8,9,6,44,66,22,12]
arr.sort()
console.log(arr); // [1, 12, 2, 22, 44, 6, 66, 8, 9]
// 如果要排列数字的话 最好用下边的俩个方法 
数组.sort(function(a,b){return a-b}) 从小到大
或者
数组.sort(function(a,b){return b-a}) 从大到小
数组名.sort(function(a,b){return a-b}); 从小到大排列
数组名.sort(function(a,b){return b-a}); 从大到小排列
 
数组名.slice(索引,个数)截取 ①索引 ② 截取的个数   [返回一个新数组]
数组名.join('')数组拼接 ('')添字符 (把数组转换成字符串)[用于把数组中的所有元素放入一个字符串]
var arr = ["hhhui", "gggg", "g", "ggg", "ddd", "ggg", "xxx", "nnn", "sss", "aaa"]
var str = arr.join(arr)
console.log(str) // 'hhhui-gggg-g-ggg-ddd-ggg-xxx-nnn-sss-aaa'
 数组的合并 concat
 var arr = [1,2,3,4,5]
 var arr1 = ['a','b','c']
 console.log(arr.concat(arr1)); //  [1, 2, 3, 4, 5, "a", "b", "c"]
数组名.forEach(function(value,index,currentArray){}) (与for用法基本一样)
 参数: value: 这个数组的每个内容
    index: 索引
    currentArray: 这个数组
数组名.filter(function(value,index,currentArray){ return 条件})  过滤出符合条件的元素,返回一个新数组
var num = [11, 22, 33, 44, 33, 66, 88, 77, 99, 2, 3, 1, 5, 4, 6, 7, 0, 9, 8]
var nums = num.filter(function(v,i,a){  return v > 11
})console.log(nums); 结果:  [22, 33, 44, 33, 66, 88, 77, 99]// currentValue  必须。当前元素的值// index 可选。当前元素的索引值// currentArray 可选。当前元素属于的数组对象
数组名.every(function(value,index,currentArray){return 条件}) 验证数组中的每个元素是否都符合指定条件,返回布尔值
     every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。
          every() 方法使用指定函数检测数组中的所有元素:
          如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
          如果所有元素都满足条件,则返回 true。
          注意: every() 不会对空数组进行检测。
          注意: every() 不会改变原始数组。
语法:
  array.every(function (currentValue, index, arr), thisValue) 
参数: 
  varlue: 当前元素的值
  index: 当前元素的索引
  arr: 当前元素属于的数组对象
thisValue: 可选, 对象作为该执行回调时使用,传递给函数, 作用"this"的值, 如果省略了thisValue, 'this'的值为'undefined'
 
 
数组名.some(function(value,index,currentArray){return 条件}) 验证数组中的每个元素是否都符合指定条件,返回布尔值 
     some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。
          some() 方法会依次执行数组的每个元素:
          如果有一个元素满足条件,则表达式返回true, 剩余的元素不会再执行检测。
          如果没有满足条件的元素,则返回false。
          注意: some() 不会对空数组进行检测。
          注意: some() 不会改变原始数组。 
语法:
array.some(function(currentValue,index,arr),thisValue)
参数: 
  value: 当前元素的值
  index: 当前元素的索引值
  arr: 当前元素属于的数组对象
thisValue: 可选, 对象作为该执行回调时使用,传递给函数, 作用"this"的值, 如果省略了thisValue, 'this'的值为'undefined'
 
 
数组.map(function(value,index,currentArray){return 操作}) 遍历数组中每一个元素,更改后存入一个新的数组中 返回新数组
          定义和用法
          map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
          map() 方法按照原始数组元素顺序依次处理元素。
          注意: map() 不会对空数组进行检测。
          注意: map() 不会改变原始数组。
语法:
array.map(function (currentValue, index, arr), thisValue)
 参数:  
  value: 当前元素的值
  index: 当前元素的索引值
  arr: 当前元素属于的数组对象
thisValue: 可选, 对象作为该执行回调时使用,传递给函数, 作用"this"的值, 如果省略了thisValue, 'this'的值为'undefined'
 
 
数组名=[] 清空数组
数组名.length = 0; 清空数组
数组名[数组.length-1] = '数据'  向数组最后追加新元素
 
isArray用来判断一个对象是不是数组, 返回值: 布尔值(true,false)
var arr = [3,4,5]
console.log(Array.isArray(arr)); // true
// ES3的判断方法
console.log(typeof arr); // object
console.log(arr.toString()); // 3,4,5
console.log(arr instanceof Array); //true
 
 
 
原文地址:https://www.cnblogs.com/maxiag/p/14817039.html