javascript数组方法

本文介绍了JavaScript数组中的四种方法:push、pop、unshift和shift。这些方法分别用于从数组的两端添加或删除元素。通过这些方法的组合使用,可以使JavaScript数组更加灵活,适用于多种数据结构需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述:(Overview:)

JavaScript arrays are comparably more versatile than arrays in other programming languages. That is because JavaScript arrays support dynamic memory by default which allows us to not worry about memory allocation. Furthermore, JavaScript arrays can be used as user-defined data structures like stacks and queues. In practice, what this means is that JavaScript arrays come with methods that allow us to insert and remove elements from either end of an array. In this article, we will discuss these methods and when best to use them.

与其他编程语言中的数组相比,JavaScript数组具有更多的通用性。 这是因为JavaScript数组默认情况下支持动态内存,这使我们不必担心内存分配。 此外,JavaScript数组可以用作用户定义的数据结构,例如堆栈和队列。 实际上,这意味着JavaScript数组带有一些方法,这些方法使我们可以从数组的任一端插入和删除元素。 在本文中,我们将讨论这些方法以及何时最佳使用它们。

推(): (Push():)

The push method adds elements to the end of an array.

push方法将元素添加到数组的末尾。

let sodas = ['coke']sodas.push('sprite') // ['coke', 'sprite']sodas.push('fanta', 'mr pibb') //['coke', 'sprite', 'fanta', 'mr pibb']

The push method also returns the new length of the array.

push方法还返回数组的新长度。

Pop(): (Pop():)

The pop method removes elements from the end of an array.

pop方法从数组末尾删除元素。

let sodas = ['coke', 'sprite', 'fanta', 'mr pibb']sodas.pop() // ['coke', 'sprite', 'fanta']

The pop method also returns the removed element. That means we can directly assign the element to another variable.

pop方法还返回移除的元素。 这意味着我们可以直接将元素分配给另一个变量。

let sodas = ['coke', 'sprite', 'fanta']let soda = sodas.pop() // 'fanta'

Unshift(): (Unshift():)

The unshift method adds an element at the beginning of an array.

unshift方法在数组的开头添加一个元素。

let sodas = ['coke', 'sprite']sodas.unshift('fanta') // ['fanta', 'coke', 'sprite']

The unshift methods, similar to the push method, returns the new length of the array.

push方法类似, unshift方法返回数组的新长度。

转移(): (Shift():)

The shift method removes an element from the beginning of an array.

shift方法从数组的开头删除元素。

let sodas = ['fanta', 'coke', 'sprite']sodas.shift() // 'fanta'

The shift method, similar to the pop method, returns the removed element from the array, and can be assigned to another variable.

pop方法类似, shift方法返回从数组中删除的元素,并且可以将其分配给另一个变量。

什么时候使用什么? (When to use what?)

The push and pop methods are used to add or remove elements from the end of an array respectively. The names of these methods are fairly straightforward as one pushes an element to the array and the other pops it off from the array.

pushpop方法分别用于从数组末尾添加或删除元素。 这些方法的名称相当简单,一个方法将元素推入数组,另一个方法将其从数组中弹出。

The shift and unshift methods add or remove items from the front of an array respectively. These method names are not as straightforward to remember. The way I remember when to use which method is by associating the length of the method name to the length of the array. The name unshift is longer which I associate with adding an element and making the array longer. Similarly, the name shift is shorter which I associate with removing an element and making the array shorter.

shiftunshift方法分别从数组的前面添加或删除项目。 这些方法名称并不容易记住。 我记得何时使用哪种方法的方法是将方法名称的长度与数组的长度相关联。 unshift的名称更长,这与我添加一个元素并使数组更长有关。 同样,名称shift更短,这与删除元素并使数组更短相关。

简而言之: (In a nutshell:)

JavaScript array methods push, pop, unshift and shift are used to add or remove elements from either end of the array. With the correct combination of these methods, we can use arrays as various user-defined data structures like stacks and queues. You can also get creative and use these methods according to your use case. I use this post as a cheat sheet and hope it helps you too.

JavaScript数组方法pushpopunshiftshift用于从数组的任一端添加或删除元素。 通过这些方法的正确组合,我们可以将数组用作各种用户定义的数据结构,例如堆栈和队列。 您还可以发挥创意,并根据用例使用这些方法。 我将此帖子用作备忘单,希望对您有所帮助。

Happy Coding!

编码愉快!

翻译自: https://medium.com/swlh/javascript-array-methods-bc19f8aeed39

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值