js数组方法

2022/2/20 23:56:23

本文主要是介绍js数组方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

es6为Array类增加了@@iterator属性,需要通过Sysbol.iterator来访问

let number = [1, 2, 3]
let iterator = number[Symbol.iterator]()
console.log(iterator.next())//{value: 1, done: false}
console.log(iterator.next())//{value: 2, done: false}
console.log(iterator.next())//{value: 3, done: false}
console.log(iterator.next())//{value: undefined, done: true}

一旦没有可迭代的值,iterator.next()就会返回value为undefoned,done为true的对象

enteries方法返回包含键值对的@@iterator

let number = [5, 4, 3]
let iterator = number.entries()//得到键值对的迭代器
console.log(iterator.next().value)//[0, 5],位置为0的值为5
console.log(iterator.next())//{value: Array(2), done: false}
console.log(iterator.next())//{value: Array(2), done: false}
console.log(iterator.next())//{value: undefined, done: true}

keys方法返回包含数组索引的@@iterator

let number = [5, 4, 3]
let iterator = number.keys()
console.log(iterator.next())//{value: 0, done: false}
console.log(iterator.next())//{value: 1, done: false}
console.log(iterator.next())//{value: 2, done: false}
console.log(iterator.next())//{value: undefined, done: true}

values方法返回包含数组的值的@@iterator

let number = [5, 4, 3]
let iterator = number.values()
console.log(iterator.next())//{value: 5, done: false}
console.log(iterator.next())//{value: 4, done: false}
console.log(iterator.next())//{value: 3, done: false}
console.log(iterator.next())//{value: undefined, done: true}

Array.from(array) 根据已有的数组创建一个新数组

 ```javascript

console.log( Array.from([1,2,3]))//[1, 2, 3]
```

==Array.of(params)==根据传入的参数创建一个新数组

console.log(Array.of(2,5,6))//[2, 5, 6]

push()

unshift()

pop()

shift()

splice()

concat()

迭代器函数:

  • every()
  • some()
  • forEach()
  • map()
  • filter()
  • reduce()

for……of

@@iterator

entries()

keys()

values()

Array.from()

Array.of()

fill()

copyWithin()

reverse()

sort()

indexOf()

lastIndexOf()

find()

findIndex()

includes()

toString()

join()



这篇关于js数组方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程