通过示例解释所有 JavaScript 数组方法( 四 )

console.log(word);// Output : hello28. reduceRight
它类似于reduce方法 , 但从数组的最后一个元素开始迭代 。
/** Reduce hello array to olleh string */const letters = ["h", "e", "l", "l", "o"];const word = letters.reduceRight((accWord, letter) => `${accWord}${letter}`);console.log(word);// Output : olleh29. reverse
反转数组元素的顺序 。
/** Reverse hello array to olleh array */const letters = ["h", "e", "l", "l", "o"];letters.reverse();console.log(letters);// Output : ["o", "l", "l", "e", "h"]30.shift
删除数组的第一个元素并返回它 。
/** Reverse number order of number array */const numbers = [1, 2, 3];numbers.reverse();console.log(numbers);// Output : [3, 2, 1];31.slice
返回从给定开始到结束索引的子数组 。
负索引表示从最后一个开始计数(例如:-1 是最后一个元素) 。
Python">const numbers = ["a", "b", "c", "d", "e"];/** Get numbers from index 1 to index 4 not included ("b", "c" and "d") */console.log(numbers.slice(1, 4));// Output : ["b", "c", "d"]/** Get numbers from index 1 to the end ("b", "c", "d" and "e") */console.log(numbers.slice(1));// Output : ["b", "c", "d", "e"]/** Get numbers from index -4 (equivalent to 1) to index -1 (equivalent to 4) not included ("b", "c" and "d") */console.log(numbers.slice(-4, -1));// Output : ["b", "c", "d"]32. some
检查是否至少有一个元素验证给定条件 。
const numbers = [10, 15, 20, 25, 30];const isAtLeastOneBelow20 = numbers.some((number) => number < 20);console.log(isAtLeastOneBelow20); // Output : trueconst isAtLeastOneBelow5 = numbers.some((number) => number < 5);console.log(isAtLeastOneBelow5); // Output : false33.  sort
通过给定的回调返回排序的数组 。
如果回调返回正数,则将 a 排序在 b 之后 。否则,将 b 排序在 a 之后 。
let numbers = [];/** Sort number in ascendent order. Sort a after b. */numbers = [10, 100, 20, 25, 30];numbers.sort((a, b) => a - b);console.log(numbers); // Output : [10, 20, 25, 30, 100]/** Sort number in descendant order. Sort b after a */numbers = [10, 100, 20, 25, 30];numbers.sort((a, b) => b - a);console.log(numbers); // Output : [100, 30, 25, 20, 10]34. splice
删除或替换从给定开始索引到给定结束索引的子数组 。
负索引表示从最后一个开始计数(例如:-1 是最后一个元素) 。
let numbers = [];/** Remove elements from index 1 to 3 elements further ([0, 0, 0]) */numbers = [1, 0, 0, 0, 1];numbers.splice(1, 3);console.log(numbers);// Output : [1, 1]/** Remove elements from index 1 to the end ([0, 0, 0, 1]) */numbers = [1, 0, 0, 0, 1];numbers.splice(1);console.log(numbers);// Output : [1]/** Remove elements from index -4 (equivalent to 1) to the end ([0, 0, 0, 1]) */numbers = [1, 0, 0, 0, 1];numbers.splice(-4);console.log(numbers);// Output : [1]/** Replace elements by 2, 2, 2 from index 1 to index 3 included ([0, 0, 0]) */numbers = [1, 0, 0, 0, 1];numbers.splice(1, 3, 2, 2, 2);console.log(numbers);// Output : [1, 2, 2, 2, 1]/** Replace elements by 2, 2, 2 from index -4 (equivalent to 1) to 3 elements further ([0, 0, 0]) */numbers = [1, 0, 0, 0, 1];numbers.splice(-4, 3, 2, 2, 2);console.log(numbers);// Output : [1, 2, 2, 2, 1]/** Add elements 2, 2, 2 at index 1 */numbers = [1, 0, 0, 0, 1];numbers.splice(1, 0, 2, 2, 2);console.log(numbers);// Output : [1, 2, 2, 2, 0, 0, 0, 1]35. toLocaleString
将所有元素转换为语言环境字符串,将所有元素连接为字符串,同时用逗号分隔每个元素并返回字符串 。
const date = [10.4, new Date("31 Aug 2022 22:00:00 UTC")];


推荐阅读