记录一下JavaScript的数组方法
不全,只是一些常用,但是容易忘记或者混淆的数组方法,后续可能会继续补充。
split()方法,将字符串以特定的元素分开,返回一个新的数组
const str = 'The quick brown fox jumps over the lazy.';
const words = str.split(' ');// 以空格分开,可以看到原字符串有空格,就会按照空格分开所有字符串中的单词,组成新数组
console.log(words); // Array ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy."]
console.log(words.length) // 8
如果是下面这样:
const words = str.split('');// 以“空”分开这个字符串,就会把字符串的每一个字符包括空格都分开成为单独的数组元素
console.log(words); // Array ["T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "s", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", "."]
console.log(words.length) // 40
如果是下面这样:
const words = str.split(',');// 可以看到,原字符串并没有“,”这个符号,所以没办法切分,只能将整个字符串当作数组元素,所以数组长度就是1
console.log(words); // Array ["The quick brown fox jumps over the lazy."]
console.log(words.length) // 1
-
join()方法,将一个数组或者类数组的对象,通过括号中的连接符,连接成一整个字符串const elements = ['Fire', 'Air', 'Water']; console.log(elements.join()); // Expected output: "Fire,Air,Water" console.log(elements.join('')); // Expected output: "FireAirWater" console.log(elements.join('-')); // Expected output: "Fire-Air-Water" -
some()方法,测试数组中是否至少有一个元素通过了由提供的函数实现的测试。如果在数组中找到一个元素使得提供的函数返回 true,则返回 true;否则返回 false。它不会修改数组。const array = [1, 2, 3, 4, 5]; // 检查数组中的某一个元素是不是偶数 const even = (element) => element % 2 === 0; console.log(array.some(even)) // 可以,下面的也可以 console.log(array.some(element => element % 2 === 0)); // Expected output: true -
reduce()方法,即一个用来累加数组的所有元素的方法,最终返回累加的结果。请看下方代码,值得注意的是,当有初始值(initialValue)时,reduce的方法accumulator(累加器)的第一个值将从initialValue开始,然后currentValue从数组的第一个值开始,见执行过程。如果没有initialValue,也就是没有指定它(无reduce的第三个参数时),reduce方法的accumulator将取数组的第一个值,也就是下面的1,currentValue即取第二个值也就是下面的2。const array1 = [1, 2, 3, 4]; // 0 + 1 + 2 + 3 + 4 const initialValue = 0; const sumWithInitial = array1.reduce( (accumulator, currentValue) => { console.log('accumulator is:',accumulator) console.log('currentValue is:',currentValue) console.log('accumulator + currentValue is:',accumulator+currentValue) return accumulator + currentValue}, initialValue, ); console.log(sumWithInitial); // Expected output: 10 // 执行过程 > "accumulator is:" 0 > "currentValue is:" 1 > "accumulator + currentValue is:" 1 > "accumulator is:" 1 > "currentValue is:" 2 > "accumulator + currentValue is:" 3 > "accumulator is:" 3 > "currentValue is:" 3 > "accumulator + currentValue is:" 6 > "accumulator is:" 6 > "currentValue is:" 4 > "accumulator + currentValue is:" 10 > 10 -
map()方法,将返回一个新的数组,与原数组不一样,不改变原数组,即遍历一下原数组中的元素,对其中的元素执行某个callback,最终返回一个新数组。 -
slice()方法,可以是截取原数组返回一个子数组(不改变原数组),也可以是原数组直接执行arr.slice(),这将会返回一个新的数组,即原数组的拷贝,不会影响原数组。 -
filter()方法,即是对原数组进行过滤,不会影响原数组,当数组中的某个元素满足某个条件时,返回当前的数组项,最终会得到一个新的过滤后的数组。 -
find()方法,即在数组中找到第一个满足条件的元素,并返回它的值,如果没有找到满足条件的元素,则返回undefined。 -
splice()方法,即在数组中添加或删除元素,改变原数组。它可以接受三个参数,第一个参数是开始修改的数组索引,第二个参数是要删除的元素数量,第三个参数是要添加的新元素(可选)。如果第二个参数为0,则表示不删除任何元素,只添加新元素;如果第三个参数没有提供,则表示只删除元素,不添加新元素。
const myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
// 从索引2开始删除0个元素,并添加'drum'
myFish.splice(2, 0, 'drum')
console.log(myFish)
// Expected output: Array ["angel", "clown", "drum", "mandarin", "sturgeon"]
// 从索引3开始删除1个元素,并添加'tulip'
myFish.splice(3, 1, 'tulip')
console.log(myFish)
// Expected output: Array ["angel", "clown", "drum", "tulip", "sturgeon"]
// 从索引3开始删除1个元素,不添加任何元素
myFish.splice(3, 1)
console.log(myFish)
// Expected output: Array ["angel", "clown", "drum", "sturgeon"]