📜  JavaScript基本数组方法

📅  最后修改于: 2021-05-20 05:55:25             🧑  作者: Mango

建议使用JavaScript中的Arrays。我们将讨论以下数组函数:

  1. Array.push():在数组末尾添加元素。由于JavaScript中的数组是可变对象,因此我们可以轻松地从Array中添加或删除元素。当我们修改数组中的元素时,它会动态变化。
    句法:
Array.push(item1, item2 …)

参数:要添加到数组的项目。

  1. 说明:此方法用于在数组末尾添加元素。
// 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);
  1. Array.unshift():在Array的前面添加元素
    句法:
Array.unshift(item1, item2 …)

参数:要添加到数组中的项目

  1. 说明:此方法用于在数组的开头添加元素。
// 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);
  1. Array.pop():从数组末尾删除元素
    句法:
Array.pop()
Parameters: It takes no parameter

  1. 说明:它用于从数组末尾删除数组元素。
// 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);
  1. Array.shift():删除数组开头的元素
    句法 :
Array.shift()
Parameter : it takes no parameter

  1. 描述:用于删除数组开头的数组。
// 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);
  1. 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 
  1. 说明:拼接是非常有用的方法,因为它可以从特定位置删除和添加元素。
// 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在数组上提供了各种功能,请参考下面的链接:

    • 功能第1部分
    • 功能第2部分
    • 功能第3部分