Heim > Artikel > Web-Frontend > Die nützlichsten JavaScript-Snippets
Steigern Sie Ihre Codierungseffizienz mit diesen wichtigen Snippets.
let randomNum = Math.floor(Math.random() * maxNum);
function isEmptyObject(obj) { return Object.keys(obj).length === 0; }
function countdownTimer(minutes) { /* countdown logic */ }
function sortByProperty(arr, property) { return arr.sort((a, b) => (a[property] > b[property]) ? 1 : -1); }
let uniqueArr = [...new Set(arr)];
function truncateString(str, num) { return str.length > num ? str.slice(0, num) + "..." : str; }
function toTitleCase(str) { return str.replace(/\b\w/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); }
let isValueInArray = arr.includes(value);
let reversedStr = str.split("").reverse().join("");
let newArr = oldArr.map(function(item) { return item + 1; });
function debounce(func, delay) { let timeout; return function(...args) { clearTimeout(timeout); timeout = setTimeout(() => func.apply(this, args), delay); }; }
function throttle(func, limit) { let lastFunc; let lastRan; return function(...args) { if (!lastRan) { func.apply(this, args); lastRan = Date.now(); } else { clearTimeout(lastFunc); lastFunc = setTimeout(function() { if ((Date.now() - lastRan) >= limit) { func.apply(this, args); lastRan = Date.now(); } }, limit - (Date.now() - lastRan)); } }; }
const cloneObject = (obj) => ({ ...obj });
const mergeObjects = (obj1, obj2) => ({ ...obj1, ...obj2 });
function isPalindrome(str) { const cleanedStr = str.replace(/[^A-Za-z0-9]/g, '').toLowerCase(); return cleanedStr === cleanedStr.split('').reverse().join(''); }
const countOccurrences = (arr) => arr.reduce((acc, val) => (acc[val] ? acc[val]++ : acc[val] = 1, acc), {});
const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
const uniqueValues = arr => [...new Set(arr)];
const degreesToRads = deg => (deg * Math.PI) / 180;
const defer = (fn, ...args) => setTimeout(fn, 1, ...args);
Das obige ist der detaillierte Inhalt vonDie nützlichsten JavaScript-Snippets. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!