涓轰簡鎻愰珮寮€鍙戞晥鐜囧苟缂栧啓鏇存竻鏅般€佸彲缁存姢鐨勪唬鐮侊紝绋嬪簭鍛樹滑缁忓父浣跨敤鍚勭鑷畾涔夊嚱鏁般€備粖澶╃粰澶у浠嬬粛20涓父鐢ㄧ殑JavaScript鍑芥暟锛屽笇鏈涘澶у鏈夊府鍔┿€?/p>
1. 娣卞害澶嶅埗瀵硅薄
娣卞害澶嶅埗瀵硅薄鏄湪JavaScript涓潪甯告湁鐢ㄧ殑鎿嶄綔锛岀壒鍒槸褰撲綘闇€瑕佸厠闅嗗璞′互閬垮厤寮曠敤闂鏃躲€?code>deepCopy鍑芥暟閫掑綊鍦板鍒跺璞$殑鎵€鏈夊睘鎬э紝纭繚涓嶄細鍏变韩寮曠敤銆?/p>
function deepCopy(obj) {
if (obj === null || typeof obj !== 'object') return obj;
const copy = Array.isArray(obj) [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key]);
}
}
return copy;
}
2. 寮傛寰幆鎵ц鏁扮粍鍏冪礌
鍦ㄥ鐞嗗紓姝ヤ换鍔℃椂锛岀粡甯搁渶瑕佸惊鐜墽琛屾暟缁勫厓绱狅紝骞剁‘淇濇瘡涓换鍔″畬鎴愬悗鎵嶆墽琛屼笅涓€涓换鍔°€?code>asyncForEach鍑芥暟鍙互甯姪浣犲疄鐜拌繖涓€鐩爣锛屼互閬垮厤鍥炶皟鍦扮嫳銆?/p>
async function asyncForEach(arr, callback) {
for (let i = 0; i < arr.length; i++) {
await callback(arr[i], i, arr);
}
}
3. 鐢熸垚鍞竴鏍囪瘑绗︼紙UUID锛?/h3>
鐢熸垚鍞竴鏍囪瘑绗﹀浜庤窡韪璞℃垨鏁版嵁闈炲父鏈夌敤銆?code>generateUUID鍑芥暟鐢熸垚绗﹀悎UUID鏍煎紡鐨勫敮涓€鏍囪瘑绗︼紝鍙敤浜庡敮涓€鏍囪瘑鏁版嵁鎴栧璞°€?/p>
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c === 'x' r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
4. 鎵佸钩鍖栧祵濂楁暟缁?/h3>
鏈夋椂锛屼綘闇€瑕佸皢宓屽鏁扮粍鎵佸钩鍖栵紝浠ヤ究鏇磋交鏉惧湴澶勭悊鏁版嵁銆?code>flattenArray鍑芥暟灏嗗祵濂楁暟缁勮浆鎹负涓€缁存暟缁勶紝浣挎暟鎹洿鏄撲簬澶勭悊銆?/p>
function flattenArray(arr) {
return arr.reduce((acc, val) => Array.isArray(val) acc.concat(flattenArray(val)) : acc.concat(val), []);
}
5. 鏌ユ壘涓や釜鏁扮粍鐨勪氦闆?/h3>
鏌ユ壘涓や釜鏁扮粍鐨勪氦闆嗘槸瑙e喅鏁版嵁澶勭悊闂鐨勪竴绉嶅父瑙佹柟寮忋€?code>arrayIntersection鍑芥暟鎺ュ彈涓や釜鏁扮粍浣滀负鍙傛暟锛屽苟杩斿洖瀹冧滑鐨勪氦闆嗐€?/p>
function arrayIntersection(arr1, arr2) {
return arr1.filter(value => arr2.includes(value));
}
6. 鏌ユ壘涓や釜鏁扮粍鐨勫樊闆?/h3>
鏈夋椂锛屼綘闇€瑕佹壘鍒颁袱涓暟缁勪箣闂寸殑宸紓銆?code>arrayDifference鍑芥暟鎺ュ彈涓や釜鏁扮粍浣滀负鍙傛暟锛屽苟杩斿洖瀹冧滑鐨勫樊闆嗐€?/p>
function arrayDifference(arr1, arr2) {
return arr1.filter(value => !arr2.includes(value));
}
7. 鍒ゆ柇鏄惁涓洪棸骞?/h3>
鍦ㄦ煇浜涙儏鍐典笅锛屼綘鍙兘闇€瑕佸垽鏂煇涓€骞存槸鍚︿负闂板勾銆?code>isLeapYear鍑芥暟鎺ュ彈涓€涓勾浠戒綔涓哄弬鏁帮紝骞惰繑鍥炲竷灏斿€硷紝鎸囩ず璇ュ勾浠芥槸鍚︿负闂板勾銆?/p>
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
8. 灏嗗嚱鏁版煰閲屽寲
鍑芥暟鏌噷鍖栨槸涓€绉嶆湁鐢ㄧ殑鎶€鏈紝鍙互灏嗕竴涓帴鍙楀涓弬鏁扮殑鍑芥暟杞寲涓轰竴绯诲垪鎺ュ彈鍗曚釜鍙傛暟鐨勫嚱鏁般€?code>curry鍑芥暟鎺ュ彈涓€涓嚱鏁颁綔涓哄弬鏁帮紝骞惰繑鍥炲叾鏌噷鍖栫増鏈€?/p>
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func(...args);
} else {
return function(...nextArgs) {
return curried(...args.concat(nextArgs));
};
}
};
}
9. 鏌ユ壘鏁扮粍涓殑鏈€澶ц繛缁瓙鏁扮粍鍜?/h3>
鍦ㄥ鐞嗘暟鍊兼暟缁勬椂锛屼綘鍙兘闇€瑕佹壘鍒板叾涓殑鏈€澶ц繛缁瓙鏁扮粍鍜屻€?code>maxSubarraySum鍑芥暟鎺ュ彈涓€涓暟缁勪綔涓哄弬鏁帮紝骞惰繑鍥炴渶澶ц繛缁瓙鏁扮粍鐨勫拰銆?/p>
function maxSubarraySum(arr) {
let maxSum = -Infinity;
let currentSum = 0;
for (let num of arr) {
currentSum = Math.max(num, currentSum + num);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
10. 璁$畻鏂愭尝閭e鏁板垪
鏂愭尝閭e鏁板垪鏄竴涓父瑙佺殑鏁板搴忓垪锛岀敤浜庢ā鎷熻嚜鐒剁晫鐨勮澶氱幇璞°€?code>fibonacci鍑芥暟鎺ュ彈涓€涓暣鏁颁綔涓哄弬鏁帮紝骞惰繑鍥炲搴斾綅缃殑鏂愭尝閭e鏁般€?/p>
function fibonacci(n) {
if (n <= 1) return n;
let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
let temp = b;
b = a + b;
a = temp;
}
return b;
}
11. 鐢熸垚鎸囧畾鑼冨洿鍐呯殑闅忔満鏁存暟
鏈夋椂浣犻渶瑕佺敓鎴愭寚瀹氳寖鍥村唴鐨勯殢鏈烘暣鏁扮敤浜庡悇绉嶇洰鐨勩€?code>getRandomInt鍑芥暟鎺ュ彈涓や釜鍙傛暟锛?code>min鍜?code>max锛屽苟杩斿洖涓€涓湪杩欎袱涓€间箣闂达紙鍖呮嫭杈圭晫锛夌殑闅忔満鏁存暟銆?/p>
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
12. 鍒ゆ柇鏄惁涓哄洖鏂囧瓧绗︿覆
鍥炴枃瀛楃涓叉槸鎸囨璇诲拰鍙嶈閮界浉鍚岀殑瀛楃涓层€?code>isPalindrome鍑芥暟鎺ュ彈涓€涓瓧绗︿覆浣滀负鍙傛暟锛屽皢鍏惰浆鎹负灏忓啓骞剁Щ闄ら潪瀛楁瘝鏁板瓧瀛楃锛岀劧鍚庢鏌ユ槸鍚︿负鍥炴枃瀛楃涓层€?/p>
function isPalindrome(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return str === str.split('').reverse().join('');
}
13. 灏嗙鏁拌浆鎹负鏃堕棿瀛楃涓?/h3>
浣犲彲鑳介渶瑕佸皢缁欏畾鐨勭鏁拌浆鎹负鏄撹鐨勬椂闂村瓧绗︿覆銆?code>secondsToTime鍑芥暟鎺ュ彈涓€涓鏁颁綔涓哄弬鏁帮紝骞跺皢鍏惰浆鎹负 鍦ㄦ煇浜涙儏鍐典笅锛屼綘鍙兘闇€瑕佹鏌ヤ竴涓€兼槸鍚︿负鏁存暟銆?code>isInteger鏃?鍒?绉?/code>鐨勬牸寮忋€?/p>
鍑芥暟鎺ュ彈涓€涓€间綔涓哄弬鏁帮紝骞惰繑鍥炲竷灏斿€硷紝鎸囩ず璇ュ€兼槸鍚︿负鏁存暟銆?/p>
function secondsToTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;
return `${hours}:${minutes}:${remainingSeconds}`;
}
14. 鍒ゆ柇鏄惁涓烘暣鏁?/h3>
function isInteger(value) {
return Number.isInteger(value);
}
15. 璁$畻瀛楃涓蹭腑鐗瑰畾瀛楃鐨勫嚭鐜版鏁?/h3>
countOccurrences
鍑芥暟鎺ュ彈涓や釜鍙傛暟锛屼竴涓槸瀛楃涓诧紝鍙︿竴涓槸瑕佽绠楃殑瀛楃锛岀劧鍚庤繑鍥炶瀛楃鍦ㄥ瓧绗︿覆涓嚭鐜扮殑娆℃暟銆?/p>
function countOccurrences(str, target) {
return str.split(target).length - 1;
}
16. 闅忔満鐢熸垚鎸囧畾鑼冨洿鍐呯殑鏁存暟
鏈夋椂锛屼綘鍙兘闇€瑕佺敓鎴愭寚瀹氳寖鍥村唴鐨勬暣鏁帮紝鑰屼笉浠呬粎鏄暣鏁般€?code>getRandomInt鍑芥暟鍙互鎺ュ彈min
鍜?code>max涓や釜鍙傛暟锛岀劧鍚庣敓鎴愪竴涓湪杩欎袱涓€间箣闂寸殑闅忔満鏁存暟銆?/p>
function getRandomInt(min, max) {
if (min > max) {
[min, max] = [max, min];
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
17. 灏嗗瓧绗︿覆鍙嶈浆
鏈夋椂锛屼綘鍙兘闇€瑕佸皢涓€涓瓧绗︿覆鍙嶈浆锛屼互瀹炵幇涓嶅悓鐨勬晥鏋溿€?code>reverseString鍑芥暟鎺ュ彈涓€涓瓧绗︿覆浣滀负鍙傛暟锛屽苟杩斿洖璇ュ瓧绗︿覆鐨勫弽杞増鏈€?/p>
function reverseString(str) {
return str.split("").reverse().join("");
}
18. 灏嗛瀛楁瘝澶у啓
鏈夋椂锛屼綘鍙兘闇€瑕佸皢瀛楃涓蹭腑鐨勯瀛楁瘝澶у啓锛屼互婊¤冻鐗瑰畾鐨勬枃鏈牸寮忋€?code>capitalizeFirstLetter鍑芥暟鎺ュ彈涓€涓瓧绗︿覆浣滀负鍙傛暟锛屽苟杩斿洖涓€涓柊瀛楃涓诧紝鍏朵腑棣栧瓧姣嶅ぇ鍐欍€?/p>
function capitalizeFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
19. 鍒ゆ柇鏁扮粍鏄惁鍖呭惈鐗瑰畾鍏冪礌
鍦ㄥ鐞嗘暟缁勬椂锛屼綘鍙兘闇€瑕佺‘瀹氭暟缁勪腑鏄惁鍖呭惈鐗瑰畾鍏冪礌銆?code>arrayContains鍑芥暟鎺ュ彈涓€涓暟缁勫拰涓€涓鏌ユ壘鐨勫厓绱犱綔涓哄弬鏁帮紝鐒跺悗杩斿洖涓€涓竷灏斿€硷紝鎸囩ず鏁扮粍鏄惁鍖呭惈璇ュ厓绱犮€?/p>
function arrayContains(arr, element) {
return arr.indexOf(element) !== -1;
}
20. 楠岃瘉鐢靛瓙閭欢鍦板潃鏍煎紡
鍦ㄥ鐞嗙敤鎴疯緭鍏ユ垨琛ㄥ崟鏁版嵁鏃讹紝浣犲彲鑳介渶瑕侀獙璇佺數瀛愰偖浠跺湴鍧€鐨勬牸寮忔槸鍚︽湁鏁堛€?code>isValidEmail鍑芥暟鎺ュ彈涓€涓數瀛愰偖浠跺湴鍧€浣滀负鍙傛暟锛屽苟浣跨敤姝e垯琛ㄨ揪寮忔潵楠岃瘉鍏舵牸寮忔槸鍚︾鍚堟爣鍑嗐€?/p>
function isValidEmail(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
Happy coding! 馃殌馃懆馃捇馃摑
鏈枃鐢眒dnice澶氬钩鍙板彂甯?/p>