数组方法的类别:
以下所有 31 种方法的示例:
const arr1 = [1, 2]; const arr2 = [3, 4]; console.log(arr1.concat(arr2)); // Output: [1, 2, 3, 4]
const fruits = ['Apple', 'Banana', 'Orange']; console.log(fruits.join(', ')); // Output: "Apple, Banana, Orange"
const numbers = [1, 2, 3, 4]; console.log(numbers.fill(0)); // Output: [0, 0, 0, 0]
const colors = ['red', 'blue', 'green']; console.log(colors.includes('blue')); // Output: true
const numbers2 = [1, 2, 3, 2]; console.log(numbers2.indexOf(2)); // Output: 1
const letters = ['a', 'b', 'c']; console.log(letters.reverse()); // Output: ['c', 'b', 'a']
const unsorted = [3, 1, 4, 1, 5]; console.log(unsorted.sort()); // Output: [1, 1, 3, 4, 5]
const months = ['Jan', 'March', 'April']; months.splice(1, 0, 'Feb'); console.log(months); // Output: ['Jan', 'Feb', 'March', 'April']
const array1 = [5, 12, 8, 130, 44]; console.log(array1.at(2)); // Output: 8
const array2 = ['a', 'b', 'c', 'd', 'e']; console.log(array2.copyWithin(0, 3, 4)); // Output: ['d', 'b', 'c', 'd', 'e']
const arr3 = [1, 2, [3, 4, [5, 6]]]; console.log(arr3.flat(2)); // Output: [1, 2, 3, 4, 5, 6]
console.log(Array.from('hello')); // Output: ['h', 'e', 'l', 'l', 'o']
const numbers3 = [5, 12, 8, 130, 44, 8]; console.log(numbers3.findLastIndex(num => num === 8)); // Output: 5
const numbers4 = [1, 2, 3]; numbers4.forEach(num => console.log(num * 2)); // Output: 2, 4, 6
const numbers5 = [1, 2, 3, 4, 5]; console.log(numbers5.every(num => num > 0)); // Output: true
const fruits2 = ['Apple', 'Banana']; const iterator = fruits2.entries(); console.log([...iterator]); // Output: [[0, 'Apple'], [1, 'Banana']]
const fruits3 = ['Apple', 'Banana']; const values = [...fruits3.values()]; console.log(values); // Output: ['Apple', 'Banana']
const arr4 = [1, 2, 3]; console.log(arr4.toReversed()); // Output: [3, 2, 1] console.log(arr4); // Original array unchanged: [1, 2, 3]
const arr5 = [3, 1, 2]; console.log(arr5.toSorted()); // Output: [1, 2, 3] console.log(arr5); // Original array unchanged: [3, 1, 2]
const arr6 = [1, 2, 3]; console.log(arr6.toSpliced(1, 1, 'two')); // Output: [1, 'two', 3] console.log(arr6); // Original array unchanged: [1, 2, 3]
const arr7 = [1, 2, 3]; console.log(arr7.with(1, 'two')); // Output: [1, 'two', 3] console.log(arr7); // Original array unchanged: [1, 2, 3]
async function* asyncGenerator() { yield 1; yield 2; } Array.fromAsync(asyncGenerator()).then(array => console.log(array)); // Output: [1, 2]
console.log(Array.of(1, 2, 3)); // Output: [1, 2, 3]
const numbers6 = [1, 2, 3]; console.log(numbers6.map(x => x * 2)); // Output: [2, 4, 6]
const arr8 = [1, 2, 3]; console.log(arr8.flatMap(x => [x, x * 2])); // Output: [1, 2, 2, 4, 3, 6]
const numbers7 = [1, 2, 3, 4]; console.log(numbers7.reduce((acc, curr) => acc + curr, 0)); // Output: 10
const numbers8 = [1, 2, 3, 4]; console.log(numbers8.reduceRight((acc, curr) => acc + curr, 0)); // Output: 10
const numbers9 = [1, 2, 3, 4, 5]; console.log(numbers9.some(num => num > 4)); // Output: true
const numbers10 = [5, 12, 8, 130, 44]; console.log(numbers10.find(num => num > 10)); // Output: 12
const numbers11 = [5, 12, 8, 130, 44]; console.log(numbers11.findIndex(num => num > 10)); // Output: 1
const numbers12 = [5, 12, 8, 130, 44]; console.log(numbers12.findLast(num => num > 10)); // Output: 44
我定期分享有关 JavaScript、Node.js、React、Next.js、软件工程、数据结构、算法等的见解。让我们一起交流、学习、成长!
跟我来:Nozibul Islam
以上是JavaScript 数组方法示例:综合指南(方法)的详细内容。更多信息请关注PHP中文网其他相关文章!