// 示例代码:JavaScript 数组的基本操作
// 创建一个数组
let arr = [1, 2, 3, 4, 5];
// 遍历数组并打印每个元素
arr.forEach(function(element) {
console.log(element);
});
// 向数组末尾添加一个元素
arr.push(6);
console.log(arr); // 输出: [1, 2, 3, 4, 5, 6]
// 从数组末尾移除一个元素
arr.pop();
console.log(arr); // 输出: [1, 2, 3, 4, 5]
// 查找数组中是否存在某个值
let index = arr.indexOf(3);
if (index !== -1) {
console.log("找到了元素 3,索引为: " + index);
} else {
console.log("未找到元素 3");
}
// 使用 map 方法对数组中的每个元素进行操作,并返回新的数组
let newArr = arr.map(function(x) {
return x * 2;
});
console.log(newArr); // 输出: [2, 4, 6, 8, 10]
[] 来创建一个数组。forEach 方法可以遍历数组中的每个元素。push 方法可以在数组末尾添加一个新元素。pop 方法可以从数组末尾移除一个元素。indexOf 方法可以查找数组中是否存在某个值,并返回其索引。map 方法可以对数组中的每个元素进行操作,并返回一个新的数组。上一篇:js ??
下一篇:js 且
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站