Home > Article > Web Front-end > JS array learning: adding elements based on array subscripts
In the previous article "How to insert elements at the head or tail of JS array learning? 》, we introduced three ways to add elements at the beginning or end of an array. This time we continue to learn about the method of adding array elements and introduce how to add elements according to the array subscript. Interested friends can learn about it~
Each element in the array has a serial number. This The serial number starts from 0 and is called the index. The subscript of the array is very important. We can use the subscript to access the array elements. We only need to specify the subscript, in the form: array name[subscript index]
. Example:
a[0] //表示第0个元素 a[3] //表示第3个元素
We can also insert elements using subscripts. Let’s introduce them in detail below.
We can use the form array name[subscript value] = element value
to add a new element at the specified subscript position of the array
var a = [1,2,3]; //定义数组 a[3] = 4; //为数组添加一个元素 console.log(a); //返回[1,2,3,4]
Look at the output result:
However, when using this method, you need to specify the subscript value >= the length of the original array, so that a new element will be added to the end of the original array
var a = [1,2,3]; //定义数组 a[4] = 4; //为数组添加一个元素 console.log(a);
Look at the output result:
##If it is less than the length of the original array, it is not adding a new element, it will become a replacement elementvar a = [1,2,3]; //定义数组 a[1] = 4; //为数组添加一个元素 console.log(a);Look at the output results: Use
Array name [subscript value] = element value can only be used in the array Add a new element at the specified subscript position; so what do you do if you want to insert one or more elements at the specified subscript position?
We can use the splice() method. splice() is a powerful method that can not only delete array elements, but also add array elements and replace array elements.
Let’s talk about the splice() method’s function of adding array elements.array.splice(index,howmany,item1,...,itemX)The first parameter
index specifies the starting subscript position; The second parameter
howmany specifies the number of elements that should be deleted. When the value is set to 0, the deletion operation will not be performed; in this way, the third and subsequent parameters
item1,... ..,itemX to insert one or more elements.
var a = [1,2,3,4,5]; a.splice(1,0,"hello"); console.log(a);It can be seen that: use
a.splice(1,0,"hello") in the subscript 1 Insert an element "
hello" at the position (that is, after the first element position of the array), so the output result is:
var a = [1,2,3,4,5]; a.splice(2,0,"hello","hi",8); console.log(a);You can see Exit: Use
a.splice(2,0,"hello","hi",8)Insert multiple elements at the position of subscript 2 (that is, after the second element position of the array)"
hello", "
hi",
8, so the output result is:
var a = [1,2,3,4,5]; a.splice(0,0,"hello"); a.splice(a.length,0,"hi"); console.log(a);If you want to add an element at the beginning of the array, just set the value of the first parameter index of this method to 0; if you want to add it at the end of the array element, just set the value of the first parameter index of the method to
array length. The array length can be obtained using
a.length.
var a = [1,2,3,4,5]; a.splice(5,0,[6,7,8,9]); console.log(a);Output result: Okay, that’s all. If you need it, you can read:
The above is the detailed content of JS array learning: adding elements based on array subscripts. For more information, please follow other related articles on the PHP Chinese website!