In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "JS how to insert elements into the specified index of the array", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "JS how to insert elements into the specified index of the array" bar!
Brief introduction
Array is a linear data structure, which can be said to be one of the most commonly used data structures in programming. Modifying arrays is a common operation, and here we discuss how to add elements anywhere in the array in JS.
Elements can be added to three locations in the array
Start / first element
End / Last element
Other places
1111
Then, let's go through one by one:
The unshift () method in the array object adds one or more elements to the beginning of the array and returns the new length of the array:
Const startArray = [3,4,5]; const newLength = startArray.unshift (2); console.log (newLength); console.log (startArray); startArray.unshift (- 1,0,2); console.log (startArray)
The output above is as follows:
4 [2,3,4,5] [- 1, 0, 2, 2, 3, 4, 5]
Add an element to the end of the array
Use the last index of the array
To add elements to the end of an array, you can use the technique that the length of the array is always 1 less than the subscript.
Const indexArray = [1,2,3]; console.log (indexArray.length); console.log (indexArray [2]); console.log (indexArray [3]); indexArray [indexArray.length] = 4 console.log (indexArray)
The output above is as follows:
3 3 undefined [1, 2, 3, 4]
The length of the array is 3 and the second element is 3. There is no third element, so we start with undefined. Finally, insert a value of 4 at that location.
Use the push () method
The push () method of the array adds one or more elements to the end of the array. Just like unshift (), it returns the new length of the array.
Const pushArray = [1,2,3] const newLength = pushArray.push (4,5,6,7); console.log (newLength); console.log (pushArray)
The output above is as follows:
7 [1, 2, 3, 4, 5, 6, 7]
Use the concat () method
Merging of two or more groups is achieved through the concat () method of the array. It creates a new copy without affecting the original array. Unlike previous methods, it returns a new array. With this method, the value to join is always at the end of the array.
Const example1Array1 = [1,2,3]; const valuesToAdd = [4,5,6]; const example1NewArray = example1Array1.concat (valuesToAdd); console.log (example1NewArray); console.log (example1Array1)
The output above is as follows:
[1, 2, 3, 4, 5, 6] [1, 2, 3]
We can connect an array to a series of values:
Const array = [1Jing 2 Jue 3]; const newArray = array.concat ('12, true, null, 4, 5, 6)); console.log (array); console.log (newArray)
The output above is as follows:
[1, 2, 3] [1, 2, 3,'12, true, null, 4, 5, 6, 'hello']
You can join an array with multiple arrays:
Const array1 = [1,2,3]; const array2 = [4,5,6]; const array3 = [7,8,9]; const oneToNine = array1.concat (array2, array3); console.log (oneToNine)
The output above is as follows:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Add an element anywhere in the array
Now we will discuss a masterstroke method that can be used to add elements anywhere in the array-- start, end, middle, and anywhere in the middle.
The splice () method adds, deletes, and replaces elements in the array. It is usually used for array management, and this method does not create a new array, but updates the array that calls it.
Let's look at the practical application of splice (). Here is a weekdays array, and now we want to add a 'Wednesday' element between Tuesday and Thursday
Const weekdays = ['Monday', 'Wednesday', 'Thursday', 'Friday'] const deletedArray = weekdays.splice (2,0, 'Tuesday'); console.log (weekdays); console.log (deletedArray)
The output above is as follows:
[Monday, Tuesday, Wednesday, Thursday, Friday] []
Analyze the code above. We want to add 'Tuesday' to the second position in the weekdays array. There is no need to delete any elements here. Weekdays.splice (2,0, 'wednesday') is read as the second location, without removing any elements and adding' Tuesday'.
Here is the general syntax for using splice ():
Let removedItems = array.splice (start [, deleteCount [, item1 [, item2 [,...])
Start- begins to modify the index of the array.
DeleteCount-the number of optional items in the array deleted from start. If omitted, all items after start will be deleted.
Item1, item2,...-optional items added to the array from start. If omitted, it only removes elements from the array.
Let's look at another example of slice (), where we add and delete arrays at the same time. We will add 'Wednesday' in the second location, but we will also delete the wrong weekend value there:
Const weekdays = ['Monday', 'Wednesday', 'Saturday', 'Sunday', 'Thursday', 'Friday'] const deletedArray = weekdays .splice (2,2, Tuesday); console.log (weekdays); console.log (deletedArray)
The output above is as follows:
["Monday", "Wednesday", "Tuesday", "Thursday", "Friday"] ["Saturday", "Sunday"] Thank you for reading. This is the content of "how JS inserts elements into the specified index of an array". After the study of this article, I believe you have a deeper understanding of how JS inserts elements into the specified index of an array. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.