有趣的JavaScript原生数组函数

web前端开发

共 1041字,需浏览 3分钟

 ·

2021-02-01 23:48

来源 | http://www.fly63.com/article/detial/9098


在JavaScript中,创建数组可以使用数组构造函数,或者使用多个直接量[],有时是首选方法。数组对象继承自Object.prototype,对操作符返回的数组类型返回对象而不是array。
然而,[]实例,数组也返回true。变量,类数组对象的实现更复杂,例如字符串对象,参数对象,参数对象不是数组的实例,但有长度属性,并能通过索引取值,所以能像多个相同进行循环操作。
在这里,我将复习一些复制原型的方法,并探索这些方法的用法。

循环:.forEach

这是JavaScript中最简单的方法,但是IE7和IE8不支持此方法。

.forEach有一个单独的函数作为参数,遍历时间表时,每个数组元素均会调用它,从而函数接受三个参数:

  • 值:当前元素

  • index:当前元素的索引

  • array:要遍历的数组

此外,可以传递可选的第二个参数,作为每次函数调用的一部分(此)。

['_', 't', 'a', 'n', 'i', 'f', ']'].forEach(function (value, index, array) {    this.push(String.fromCharCode(value.charCodeAt() + index + 2))}, out = [])out.join('')// <- 'awesome'

后文会备注.join,在这个示例中,它用于拆分分段中的不同元素,效果合并out [0] +” + out [1] +” + out [2] +” + out [n ]。

不能中断。forEach循环,并且抛出异常也是不明智的选择。幸运的事我们有另外的方式来中断操作。

判断:.some和.every

如果你用过.NET中的枚举,这两个方法和.Any(x => x.IsAwesome),.All(x => x.IsAwesome)类似。
和.forEach的参数类似,需要一个包含值,index,和array三个参数的参数函数,并且也有一个可选的第二个参数。MDN对.some的描述如下:

如果找到目标元素,一些立即返回true,否则一些返回false。某些函数只对已经指定值的排序索引执行;它不正极已删除的或未指定值的元素调用。
max = -Infinitysatisfied = [10, 12, 10, 8, 5, 23].some(function (value, index, array) {    if (value > max) max = value    return value < 10})console.log(max)// <- 12satisfied// <- true

注意,当某些函数的值<10时,中断函数循环。.every的运行原理和.some类似,但大部分函数是返回false而不是true。

区分.join和.concat

.join和.concat经常替换。.join(separator)以separator作为分隔符分割的数组元素,并返回串联形式,如果没有提供分隔符,将使用替换的,.. concat会创建一个新副本,作为源数组的浅拷贝。

  • .concat常用用法:array.concat(val,val2,val3,valn)

  • .concat返回一个新副本

  • array.concat()在没有参数的情况下,返回源数组的浅拷贝。

浅拷贝意味着新的副本和原始副本保持相同的对象引用,这通常是好事。例如:

var a = { foo: 'bar' }var b = [1, 2, 3, a]var c = b.concat()console.log(b === c)// <- falseb[3] === a && c[3] === a// <- true

栈和高度的实现:.pop,.push,.shift和.unshift

每个人都知道.push可以再添加末尾添加元素,但是你知道可以使用[] .push('a','b','c','d','z')一次性添加多个元素吗?

.pop方法是.push的反操作,它返回被删除的副本末尾元素。如果数组为空,将返回void 0(未定义),使用.pop和.push可以创建LIFO(后进先出)栈。

function Stack () {    this._stack = []}Stack.prototype.next = function () {    return this._stack.pop()}Stack.prototype.add = function () {    return this._stack.push.apply(this._stack, arguments)}stack = new Stack()stack.add(1,2,3)stack.next()// <- 3相反,可以使用.shift和 .unshift创建FIFO (first in first out)队列。
function Queue () { this._queue = []}Queue.prototype.next = function () { return this._queue.shift()}Queue.prototype.add = function () { return this._queue.unshift.apply(this._queue, arguments)}queue = new Queue()queue.add(1,2,3)queue.next()// <- 1Using .shift (or .pop) is an easy way to loop through a set of array elements, while draining the array in the process.list = [1,2,3,4,5,6,7,8,9,10]while (item = list.shift()) { console.log(item)}list// <- []

模型映射:.map

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。调用。

Array.prototype.map和上面提到的.forEach,.some和.every有相同的参数格式:.map(fn(value,index,array),thisArgument)

values = [void 0, null, false, '']values[7] = void 0result = values.map(function(value, index, array){    console.log(value)    return value})// <- [undefined, null, false, '', undefined × 3, undefined]

undefined×3非常地解释了.map不会对已删除的或未指定值的元素调用,但仍然会被包含在结果数组中。.map在创建或更改排序时非常有用,看下面的示例:

// casting[1, '2', '30', '9'].map(function (value) {    return parseInt(value, 10)})// 1, 2, 30, 9[97, 119, 101, 115, 111, 109, 101].map(String.fromCharCode).join('')// <- 'awesome'// a commonly used pattern is mapping to new objectsitems.map(function (item) {    return {        id: item.id,        name: computeName(item)    }})

查询:.filter

filter对每个数组元素执行一次变量函数,并返回一个由变量函数返回true的元素组成的新数组。这些函数只会对已经指定值的数组项调用。

通常用法:.filter(fn(value,index,array),thisArgument),跟C#中的LINQ表达式和SQL中的语句类似,.filter只返回在函数中返回真值的元素。

[void 0, null, false, '', 1].filter(function (value) {    return value})// <- [1][void 0, null, false, '', 1].filter(function (value) {    return !value})// <- [void 0, null, false, '']

排序:.sort(compareFunction)

例如,“ 80”排在“ 9”之前,而不是在其后。

跟大多数排序函数类似,Array.prototype.sort(fn(a,b))需要一个包含两个测试参数的变量函数,其返回值如下:

  • a在b之前则返回值小于0

  • a和b补充则返回值是0

  • a在b之后则返回值小于0

[9,80,3,10,5,6].sort()// <- [10, 3, 5, 6, 80, 9][9,80,3,10,5,6].sort(function (a, b) {    return a - b})// <- [3, 5, 6, 9, 10, 80]

计算:.reduce和.reduceRight

这两个函数比较难理解,.reduce会从左往右遍历数组,而.reduce右从从右往左遍历数组,各自的用法:.reduce(回调(previousValue,currentValue,index,array),initialValue) 。

previousValue是最后一次调用初始函数的返回值,initialValue则是其初始值,currentValue是当前元素值,index是当前元素索引,array是调用.reduce的数组。

一个典型的用例,使用.reduce的求和函数。

Array.prototype.sum = function () {    return this.reduce(function (partial, value) {        return partial + value    }, 0)};[3,4,5,6,10].sum()// <- 28

如果想把碎片拼接成一个串联,可以用.join实现。然而,若拆分值是对象,.join就不会按照我们的期望返回值了,除非对象有合理的valueOf或toString方法,在这种情况下情况下,可以用.reduce实现:

function concat (input) {    return input.reduce(function (partial, value) {        if (partial) {            partial += ', '        }        return partial + value    }, '')}concat([    { name: 'George' },    { name: 'Sam' },    { name: 'Pear' }])// <- 'George, Sam, Pear'

复制:.slice

和.concat类似,调用没有参数的.slice()方法会返回源数组的一个浅拷贝。.slice有两个参数:一个是开始位置和一个结束位置。Array.prototype.slice可以被当作
将类副本对象转换为真正的副本。

Array.prototype.slice.call({ 0: 'a', 1: 'b', length: 2 })// <- ['a', 'b']这对.concat不适用,因为它会用数组包裹类数组对象。
Array.prototype.concat.call({ 0: 'a', 1: 'b', length: 2 })// <- [{ 0: 'a', 1: 'b', length: 2 }]

此外,.slice的另一个通常用法是从一个参数列表中删除一些元素,这可以将类数组对象转换为真正的数组。

function format (text, bold) {    if (bold) {        text = '' + text + ''    }    var values = Array.prototype.slice.call(arguments, 2)    values.forEach(function (value) {        text = text.replace('%s', value)    })    return text}format('some%sthing%s %s', true, 'some', 'other', 'things')

强大的.splice

.splice是我最喜欢的原生数组函数,只需要调用一次,就可以让你删除元素,插入新的元素,并能同时进行删除,插入操作。需要注意的是,.`concat和.slice,这个函数会改变源矩阵。

var source = [1,2,3,8,8,8,8,8,9,10,11,12,13]var spliced = source.splice(3, 4, 4, 5, 6, 7)console.log(source)// <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13]spliced// <- [8, 8, 8, 8]

正如你看到的,.splice会返回删除的元素。如果你想遍历已经删除的副本时,这会非常方便。

var source = [1,2,3,8,8,8,8,8,9,10,11,12,13]var spliced = source.splice(9)spliced.forEach(function (value) {    console.log('removed', value)})// <- removed 10// <- removed 11// <- removed 12// <- removed 13console.log(source)// <- [1, 2, 3, 8, 8, 8, 8, 8, 9]

发现:.indexOf

利用.indexOf可以在层叠中查找一个元素的位置,没有匹配元素则返回-1。我经常使用.indexOf的情况是当我有比较时,例如:a ==='a'|| a ==='b'|| a ==='c',或者只有两个比较,此时,可以使用.indexOf:['a','b','c']。indexOf(a)!== -1。

注意,如果提供的引用相同,.indexOf也能查找对象。第二个可选参数指定指定开始查找的位置。

var a = { foo: 'bar' }var b = [a, 2]console.log(b.indexOf(1))// <- -1console.log(b.indexOf({ foo: 'bar' }))// <- -1console.log(b.indexOf(a))// <- 0console.log(b.indexOf(a, 1))// <- -1b.indexOf(2, 1)// <- 1

如果你想从后向前搜索,可以使用.lastIndexOf。

操作符:in

在面试中新手容易犯的错误是重复.indexOf和in操作符:

var a = [1, 2, 5]1 in a// <- true, but because of the 2!5 in a// <- false

当然,这在性能上比.indexOf快复杂。

var a = [3, 7, 6]1 in a === !!a[1]// <- true

走近.reverse

该方法将矩阵中的元素倒置。

var a = [1, 1, 7, 8]a.reverse()// [8, 7, 1, 1]

.reverse会修改尺寸本身。

本文完〜

浏览 9
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报