概述:(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.
push
和pop
方法分别用于从数组末尾添加或删除元素。 这些方法的名称相当简单,一个方法将元素推入数组,另一个方法将其从数组中弹出。
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.
shift
和unshift
方法分别从数组的前面添加或删除项目。 这些方法名称并不容易记住。 我记得何时使用哪种方法的方法是将方法名称的长度与数组的长度相关联。 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数组方法push
, pop
, unshift
和shift
用于从数组的任一端添加或删除元素。 通过这些方法的正确组合,我们可以将数组用作各种用户定义的数据结构,例如堆栈和队列。 您还可以发挥创意,并根据用例使用这些方法。 我将此帖子用作备忘单,希望对您有所帮助。
Happy Coding!
编码愉快!
翻译自: https://medium.com/swlh/javascript-array-methods-bc19f8aeed39