JavaScript中的数组方法可以通过数组对象来调用。以下是一些常用的数组方法的使用示例:
var fruits = ["apple", "banana"];
fruits.push("orange");
console.log(fruits); // 输出: ["apple", "banana", "orange"]
var fruits = ["apple", "banana", "orange"];
var removedFruit = fruits.pop();
console.log(fruits); // 输出: ["apple", "banana"]
console.log(removedFruit); // 输出: "orange"
var fruits = ["apple", "banana", "orange"];
var removedFruit = fruits.shift();
console.log(fruits); // 输出: ["banana", "orange"]
console.log(removedFruit); // 输出: "apple"
var fruits = ["banana", "orange"];
fruits.unshift("apple");
console.log(fruits); // 输出: ["apple", "banana", "orange"]
var fruits = ["apple", "banana", "orange"];
fruits.splice(1, 1); // 从索引1开始删除1个元素
console.log(fruits); // 输出: ["apple", "orange"]
fruits.splice(1, 0, "kiwi", "grape"); // 从索引1开始删除0个元素,并插入新元素
console.log(fruits); // 输出: ["apple", "kiwi", "grape", "orange"]
var fruits = ["apple", "banana", "orange", "kiwi", "grape"];
var selectedFruits = fruits.slice(1, 4);
console.log(selectedFruits); // 输出: ["banana", "orange", "kiwi"]
这些只是一些常见的数组方法的使用示例,JavaScript中还有很多其他有用的数组方法,可以根据具体需求查阅文档。