JavaScript ist eine funktionale Programmiersprache und Funktionen spielen eine entscheidende Rolle. Sie ermöglichen es Ihnen, wiederverwendbaren Code zu kapseln und bestimmte Aufgaben auszuführen. Hier sind einige kurze Beispiele für Funktionen, die Ihnen das Leben erleichtern können:
Regelmäßige Funktion
function sum(a, b) { return a + b; }
Funktionsausdruck
const sum = function (a, b) { return a + b; };
Pfeilfunktion
const sum = (a, b) => { return a + b; }; // OR const sum = (a, b) => a + b;
Generatorfunktion
function* indexGenerator() { let index = 0; while (true) { yield index++; } } const g = indexGenerator(); console.log(g.next().value); // => 0 console.log(g.next().value); // => 1
Erstellen Sie ein Array mit Zahlen von 1 bis n
const range = (n) => Array.from({ length: n }, (_, i) => i + 1); console.log(range(10)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Erstellen Sie mit einem Schritt ein Array von Zahlen von 1 bis n
const range = (n, step = 1) => Array.from({ length: n }, (_, i) => i * step); console.log(range(10, 2)); // [1, 3, 5, 7, 9]
Erstellen Sie ein Array und füllen Sie es mit einem Wert
const fill = (len, value) => Array(len).fill(value); console.log(fill(3, 0)); // [0, 0, 0]
Mischen eines Arrays
const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random()); console.log(shuffleArray([1, 2, 3, 4])); // [3, 2, 1, 4]
Duplikat aus Array entfernen
const removeDuplicated = (arr) => [...new Set(arr)]; console.log(removeDuplicated([1, 2, 3, 3, 4, 4, 5, 5, 6])); // Result: [ 1, 2, 3, 4, 5, 6 ] // OR const removeDuplicate = (arr) => Object.values(arr.reduce((a, b) => (a[b] ? a : { ...a, [b]: b }), {})); console.log(removeDuplicate([1, 2, 3, 3])); // Result: [ 1, 2, 3, ]
Zufallszahl generieren
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; console.log(random(1, 10)); // Result: 1 ~ 10
Finden Sie die größten Zahlen
const findLargest = (arr) => arr.map((subArr) => Math.max(...subArr)); console.log( findLargest([ [4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1], ]) ); // [5, 27, 39, 1001]
Finden Sie die kleinsten Zahlen
const findSmallest = (arr) => arr.map((subArr) => Math.min(...subArr)); console.log( findSmallest([ [4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1], ]) ); // [1, 18, 32, 857]
Wählen Sie ein zufälliges Element aus einem Array aus
const pick = (arr) => arr[Math.floor(Math.random() * arr.length)]; console.log(pick([1, 2, 3, 4])); // 2
Array in Objekt konvertieren
const toObject = (arr) => ({ ...arr }); console.log(toObject(["a", "b"])); // { 0: 'a', 1: 'b' }
Finden Sie den Schnittpunkt zweier Arrays
const intersection = (arr1, arr2) => { const set = new Set(arr1); return arr2.filter((x) => set.has(x)); }; console.log(intersection([1, 2, 3], [2, 3, 4])); // [2, 3]
Entfernen Sie falsche Werte aus einem Array
const compact = (arr) => arr.filter(Boolean); console.log(compact([0, 1, false, 2, "", 3, "a", "e" * 23, NaN, "s", 34])); // [1, 2, 3, 'a', 's', 34]
Reverse String
const reverseString = (str) => str.split("").reverse().join(""); console.log(reverseString("hello")); // olleh
Ist String Palindrome
const isPalindrome = (str) => str === str.split("").reverse().join(""); console.log(isPalindrome("madam")); // true
Überprüfen Sie, ob das Objekt leer ist oder nicht
const isEmpty = (obj) => Object.keys(obj).length === 0; console.log(isEmpty({})); // true
Ermitteln Sie die Anzahl der Tage in einem Monat
const getDaysInMonth = (date) => new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); console.log(getDaysInMonth(new Date())); // 31
Erzeugen Sie eine zufällige Farbe
const getRandomColor = () => `#${Math.floor(Math.random() * 16777215).toString(16)}`; console.log(getRandomColor()); // #f0f0f0 // OR const randomHex = () => `#${Math.floor(Math.random() * 0xffffff) .toString(16) .padEnd(6, "0")}`; console.log(randomHex()); // #f0f0f0
Schauen Sie sich das Javascript Quick Functions GitHub-Repo für weitere Funktionen wie diese an.
以上是JavaScript-Funktionen zur Vereinfachung Ihres Codes | JavaScript-Funktionen | JavaScript-Tutorial的详细内容。更多信息请关注PHP中文网其他相关文章!