数组方法:
-
将数组拼接成字符串 .join('')
-
合并数组,不修改原数组,返回新数组 arr3 = arr1.concat(arr2)
(345他三好像,注意区分)
-
将原数组的一部分复制到数组的另一个位置,修改原数组,不改变数组长度 array1.copyWithin(index, start, end)
-
将指定数值填充到数组中指定位置 .fill(value,start,end) // start默认为0,end默认为this.length
-
将数组按照索引(begin,end)截取 arr.slice()
-
测试一个数组中是不是至少有一个元素满足某个指定元素,返回布尔值,如果是空数组 返回值是false .some()
-
测试一个数组中所有元素都能通过某个指定函数,返回布尔值 .every()
const isBelowThreshold = (currentValue) => currentValue < 40; const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every(isBelowThreshold)); // expected output: true const isBelowThreshold = (currentValue) => currentValue < 40 && currentValue > 29; const array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every(isBelowThreshold)); // expected output: false 箭头函数格式: var 方法名 = (参数,参数)=> 表达式/{方法体}
-
测试数组中元素通过某个函数,返回数组 .filter()
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
-
返回数组中满足函数的第一个数值 .find()
const array1 = [5, 12, 8, 130, 44] const found = array1.find(element => element > 10); console.log(found); // expected output: 12
-
返回数组中满足函数的第一个索引值,若没有(-1) .findIndex()
array1.findIndex(element => element > 10)
-
深度递归数组并合并 .flat(n) 默认是1,一层数组
const arr1 = [0, 1, 2, [3, 4]]; console.log(arr1.flat()); // expected output: [0, 1, 2, 3, 4] const arr2 = [0, 1, 2, [[[3, 4]]]]; console.log(arr2.flat(2)); // expected output: [0, 1, 2, [3, 4]]
-
遍历数组的方法 => map 和 forEach:map有返回值,forEach没有
forEach:
const arr = ['a', 'b', 'c']; arr.forEach((value,index) => { console.log(value+index) });
Array.from
const arr = ['a', 'b', 'c']; console.log(Array.from(arr, x => x + x))
map
两种格式: const array1 = [1, 4, 9, 16]; const map1 = array1.map({ return x = x * 2 }); console.log(map1); // expected output: Array [2, 8, 18, 32] var a = [1,2,3,4,5] var b = a.map((item) => { return item = item * 2 }) console.log(a) // [1,2,3,4,5] console.log(b) // [2,4,6,8,10]
创建一个数量可变的新数组 Array.of(元素) Array.of(元素,元素)
Array(长度) Array(元素,元素)
Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] 注意:7个空位(empty)的数组,而不是由7个undefined组成的数组 Array(1, 2, 3); // [1, 2, 3]
将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组) arr.unshift()
const array1 = [1, 2, 3]; console.log(array1.unshift(4, 5)); // expected output: 5 console.log(array1); // expected output: Array [4, 5, 1, 2, 3]
从数组中删除第一个元素并返回第一位的值,修改原数组长度 arr.shift()
const array1 = [1, 2, 3]; const firstElement = array1.shift(); console.log(array1); // expected output: Array [2, 3] console.log(firstElement); // expected output: 1
-
从数组中删除最后一位元素,并返回最后一位的值,修改原数组长度 arr.pop()
[ arr.splice(-1) :删除最后一位元素 ]
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; console.log(plants.pop()); // expected output: "tomato" console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"] plants.pop(); console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage"]
-
向数组尾部插入元素,并返回新数组的长度 arr.push(元素)
const animals = ['pigs', 'goats', 'sheep']; const count = animals.push('cows'); console.log(count); // expected output: 4
-
reduce() 跳过
-
将数组倒序,会改变原数组 arr.reverse()
-
按照元素排序 ,utf-16(字母按位从小到大,数字按位从小到大) arr.sort()
-
通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组 array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
arr.splice(-1) :删除最后一位元素
-
数组转化为字符串 arr.toString()
-
for ... in
是为遍历对象属性而构建的,不建议与数组一起使用,对象可以用
数字计算方法
-
四舍五入 Math.round()
-
向上取整 Math.ceil()
-
向下取整 Math.floor()