JavaScript学习笔记06--数组操作
1.数组定义的两种方式
1.1 直接定义
const arr1 = ['yu', 'xian', 'cool'];
console.log(arr1);
1.2 Array定义
const arr2 = new Array('really', 'nice');
console.log(arr2);
2.数组的一些函数方法
2.1 push()
增加一个元素到数组末尾。返回值为新数组的元素个数。
const arr1 = ['yu', 'xian', 'cool'];
arr1.push("add");
console.log(arr1);
2.2 unshift()
增加一个元素到数组开头。返回值为新数组的元素个数。
const arr1 = ['yu', 'xian', 'cool'];
arr1.push("add");
arr1.unshift("top");
console.log(arr1);
2.3 pop()
删除数组最后一个元素。返回值为已删除的元素。
const arr1 = ['yu', 'xian', 'cool'];
const popped = arr1.pop();
console.log(arr1);
console.log(popped);
2.4 shift()
删除数组第一个元素。返回值为已删除的元素。
const arr2 = new Array('really', 'nice');
const shifted = arr2.shift();
console.log(arr2);
console.log(shifted);
2.5 indexof()
返回某元素的索引。如果数组中不存在该元素则返回 -1 。数组的起始位序号为0。
const arr3 = new Array('really', 'nice');
console.log(arr3.indexOf('really'));
2.6 includes()
返回值类型为布尔值,判断是否包含某元素。该函数并不会进行强制类型转换,不会识别 23 与 ‘23’。
const arr3 = new Array('really', 'nice');
console.log(arr3.includes('really'));
2.7 concact()
concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);

浙公网安备 33010602011771号