Heim > Artikel > Web-Frontend > Beispiele für JavaScript-Array-Methoden: Eine umfassende Anleitung (Methoden)
Kategorien von Array-Methoden:
Beispiele aller 31 Methoden unten:
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
Ich teile regelmäßig Einblicke in JavaScript, Node.js, React, Next.js, Softwareentwicklung, Datenstrukturen, Algorithmen und mehr. Lasst uns zusammenkommen, lernen und wachsen!
Folgen Sie mir: Nozibul Islam
Das obige ist der detaillierte Inhalt vonBeispiele für JavaScript-Array-Methoden: Eine umfassende Anleitung (Methoden). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!