美文网首页
2021-04-02

2021-04-02

作者: 小王子__ | 来源:发表于2021-04-02 15:27 被阅读0次

Js数组常用的方法

  • 1,push 后增,数组末尾添加元素,并返回数组的最新长度
var a = [1, 2, 3]
var b = a.push(4)
console.log(a) // [1,2,3,4]
console.log(b) // 4
  • 2,pop 后删,数组末尾删除元素,返回被删元素
var a = [1, 2, 3]
var b = a.pop()
console.log(a) // [1,2]
console.log(b) // 3
  • 3,unshift 前增,数组开头添加,返回数组的最新长度
var a = [1, 2, 3]
var b = a.unshift(0)
console.log(a) // [0,1,2,3]
console.log(b) // 4
  • 4,shift 前删,数组开头删除,返回被删元素
var a = [1, 2, 3]
var b = a.shift()
console.log(a) // [2,3]
console.log(b) // 1
  • 5,splice 修改删除
    splice(index, length, 增加的元素1,增加的元素2,...,增加的元素n)

表示从index开始删除length个元素,并从index开始新增元素1~N,放回被删除的数组里

对数组进行删除修改,返回被删除的元素组成的数组,改变原数组

var a = [1, 2, 3, 4, 5, 6]
var b = a.splice(1, 3, 100, 200, 300)
console.log(a) // [1, 100, 200, 300, 5, 6]
console.log(b) // [2, 3, 4]
  • 6,slice 剪切
    slice(startIndex, endIndex) 返回从startIndex开始(包括),到endIndex(不包括)之间的元素组成的数组

返回新数组,不改变原数组

var a = [1, 2, 3]
var b = a.slice(0, 2)
var c = a.slice()
console.log(a) // [1,2,3]
console.log(b) // [1,2]
console.log(c) // [1,2,3]
  • 7,join 数组转为字符串
    不改变原数组,返回转换后的字符串
var a = [1, 2, 3, 4, 5, 6]
console.log(a.join('|')) // 1|2|3|4|5|6
console.log(a) // [1, 2, 3, 4, 5, 6]
  • 8,concat 拼接
    用来合并两个或多个数组

返回新数组,不改变原数组

var a = [1, 2, 3]
var b = [4, 5, 6]
var c = a.concat(b)
console.log(a) // [1,2,3]
console.log(b) // [4,5,6]
console.log(c) // [1,2,3,4,5,6]
  • 9,sort 排序
    按ascii码排序,改变原数组,返回排序后的数组
var a = ['a', 'b', 'd', 'c']
var b = a.sort()
console.log(a) // ['a', 'b', 'c', 'd']
console.log(b) // ['a', 'b', 'c', 'd']
  • 10,reverse颠倒顺序
    返回的是颠倒后的数组,改变原数组
var a = [1, 2, 3, 4, 5, 6]
var b = a.reverse()
console.log(a) // [6, 5, 4, 3, 2, 1]
console.log(b) // [6, 5, 4, 3, 2, 1]
  • 11,indexOf和 lastIndexOf
    indexOf(某元素,startIndex) 从startIndex开始,查找某元素在数组中首次出现的位置,不存在返回-1

lastIndexOf(某元素,startIndex)和indexOf相同,区别在于从尾部向首部查询

不改变原数组,返回找到的index,否则返回-1

若不使用下标,一般用includes代替indexOf

var str = 'JerryJerry'
console.log(str.indexOf('r')) // 2
console.log(str.lastIndexOf('J')) // 4
  • 12,filter过滤
    返回数组中满足条件的元素组成的新数组,不改变原数组
var a = [1, 2, 3, 4, 5, 6, 7, 8]
var b = a.filter((current, index, array) => {
  return current > 4
})
console.log(a) // [1, 2, 3, 4, 5, 6, 7, 8]
console.log(b) // [5, 6, 7, 8]
  • 13,map格式化数组,改变内容
    根据需求格式化原数组,返回格式化后的数组,不改变原数组
var a = [1, 2, 3, 4, 5, 6]
var b = a.map((current, index, array) => {
  return current + 1
})
console.log(a) // [1,2,3,4,5,6]
console.log(b) // [2,3,4,5,6,7]
  • 14,every
    对数组的每一项都运行给定的函数,若每一项都返回true则返回true,任一不符合返回false
var a = [2, 3, 4, 5, 6, 7, 8]
var b = a.every((cur, idx, ary) => {
  return cur > 1
})
var c = a.every((cur, idx, ary) => {
  return cur < 6
})
console.log(a) // [1,2,3,4,5,6,7,8]
console.log(b) // true
console.log(c) // false
  • 15,some
    对数组的每一项都运行给定的函数,若存在一项或多项返回true,则返回true,任一项符合返回true
var a = [1, 2, 3, 4, 5, 6]
var b = a.some((cur, idx, ary) => {
  return cur > 3
})
var c = a.some((cur, idx, ary) => {
  return cur < 1
})
console.log(a) // [1, 2, 3, 4, 5, 6]
console.log(b) // true
console.log(c) // false
  • 16.forEach 数组遍历
var arr = [100, 200, 300, 400]
arr.forEach(e => console.log(e)) // 100 200 300 400
  • 17,toString
    JS中的所有对象都具有toString方法,它把一个变量隐式转为字符串
var arr = [100, 200, 300]
var str = arr.toString()
console.log(arr) // [100, 200, 300]
console.log(str) // 100, 200, 300
  • 18,slice
    从已有的数组中返回指定的元素 不改变原数组

slice(start, end) start开始下标 => end结束下标(不含)

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var str = arr.slice(2, 5)
console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(str) // [3, 4, 5]

相关文章

网友评论

      本文标题:2021-04-02

      本文链接:https://www.haomeiwen.com/subject/oacqkltx.html