建议通过JavaScript中的数组。我们将讨论以下数组函数:
Array.push():
在数组末尾添加元素。由于JavaScript中的数组是可变对象, 因此我们可以轻松地从Array中添加或删除元素。当我们修改数组中的元素时, 它会动态变化。
句法 :
Array.push(item1, item2 …)
Parameters: Items to be added to an array.
描述:此方法用于在数组末尾添加元素。
// Adding elements at the end of an array
// Declaring and initializing arrays
var number_arr = [ 10 , 20 , 30 , 40 , 50 ];
var string_arr = [ "piyush" , "gourav" , "smruti" , "ritu" ];
// push()
// number_arr contains [10, 20, 30, 40, 50, 60]
number_arr.push( 60 );
// We can pass multiple parameters to the push()
// number_arr contains
// [10, 20, 30, 40, 50, 60, 70, 80, 90]
number_arr.push( 70 , 80 , 90 );
// string_arr contains
// ["piyush", "gourav", "smruti", "ritu", "sumit", "amit"];
string_arr.push( "sumit" , "amit" );
// Printing both the array after performing push operation
console.log( "After push op " + number_arr);
console.log( "After push op " + string_arr);
Array.unshift():
在数组的前面添加元素
句法 :
Array.unshift(item1, item2 …)
Parameters: Items to be added to the array
描述:此方法用于在数组的开头添加元素。
// Adding element at the beginning of an array
// Declaring and initializing arrays
var number_arr = [ 20 , 30 , 40 ];
var string_arr = [ "amit" , "sumit" ];
// unshift()
// number_arr contains
// [10, 20, 20, 30, 40]
number_arr.unshift( 10 , 20 );
// string_arr contains
// ["sunil", "anil", "amit", "sumit"]
string_arr.unshift( "sunil" , "anil" );
// Printing both the array after performing unshift operation
console.log( "After unshift op " + number_arr);
console.log( "After unshift op " + string_arr);
Array.pop():
从数组末尾删除元素
语法如下:
Array.pop()
Parameters: It takes no parameter
描述:它用于从数组末尾删除数组元素。
// Removing elements from the end of an array
// Declaring and initializing arrays
var number_arr = [ 20 , 30 , 40 , 50 ];
var string_arr = [ "amit" , "sumit" , "anil" ];
// pop()
// number_arr contains
// [ 20, 30, 40 ]
number_arr.pop();
// string_arr contains
// ["amit", "sumit"]
string_arr.pop();
// Printing both the array after performing pop operation
console.log( "After pop op " + number_arr);
console.log( "After popo op " + string_arr);
Array.shift():
删除数组开头的元素
句法 :
Array.shift()
Parameter : it takes no parameter
说明:它用于删除数组开头的数组。
// Removing element from the beginning of an array
// Declaring and initializing arrays
var number_arr = [ 20 , 30 , 40 , 50 , 60 ];
var string_arr = [ "amit" , "sumit" , "anil" , "prateek" ];
// shift()
// number_arr contains
// [30, 40, 50, 60];
number_arr.shift();
// string_arr contains
// ["sumit", "anil", "prateek"]
string_arr.shift();
// Printing both the array after performing shifts operation
console.log( "After shift op " + number_arr);
console.log( "After shift op " + string_arr);
Array.splice():
在阵列之间插入和移除
语法如下:
Array.splice (start, deleteCount, item 1, item 2….)
Parameters:
Start : Location at which to perform operation
deleteCount: Number of element to be deleted, if no element is to be deleted pass 0.
Item1, item2 …..- this is an optional parameter .
These are the elements to be inserted from location start
描述:拼接是非常有用的方法, 因为它可以从特定位置删除和添加元素。
// Removing an adding element at a particular location
// in an array
// Declaring and initializing arrays
var number_arr = [ 20 , 30 , 40 , 50 , 60 ];
var string_arr = [ "amit" , "sumit" , "anil" , "prateek" ];
// splice()
// deletes 3 elements starting from 1
// number array contains [20, 60]
number_arr.splice( 1 , 3 );
// doesn't delete but inserts 3, 4, 5
// at starting location 1
number_arr.splice( 1 , 0 , 3 , 4 , 5 );
// deletes two elements starting from index 1
// and add three elements.
// It contains ["amit", "xyz", "geek 1", "geek 2", "prateek"];
string_arr.splice( 1 , 2 , "xyz" , "geek 1" , "geek 2" );
// Printing both the array after performing splice operation
console.log( "After splice op " + number_arr);
console.log( "After splice op " + string_arr);
JavaScript在数组上提供了各种功能, 请参考下面的链接:
注意 :可以通过在HTML的脚本标记中键入它们或直接在浏览器的控制台中对上述所有示例进行测试。
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。