您可以在 repo Github 上找到這篇文章中的所有程式碼。
陣列原型相關的挑戰
Array.prototype.at()
/** * @param {number} index * @return {any | undefiend} */ Array.prototype.myAt = function (index) { const len = this.length; if (index = len) { return; } return this[(index + len) % len]; }; // Usage example console.log([1, 2, 3, 4].myAt(2)); // => 3 console.log([1, 2, 3, 4].myAt(-1)); // => 4 console.log([1, 2, 3, 4].myAt(5)); // => undefined
Array.prototype.concat()
/** * @template T * @param {...(T | Array<t>)} itemes * @return {Array<t>} */ Array.prototype.myConcat = function (...items) { const newArray = [...this]; for (const item of items) { if (Array.isArray(item)) { newArray.push(...item); } else { newArray.push(item); } } return newArray; }; // Usage example console.log([1, 2, 3].myConcat([])); // => [1, 2, 3]; console.log([1, 2, 3].myConcat([4, 5, 6, [2]])); // => [1, 2, 3, 4, 5, 6, [2]]; </t></t>
Array.prototype.every()
/** * @template T * @param { (value: T, index: number, array: Array<t>) => boolean } callbackFn * @param {any} [thisArg] * @return {boolean} */ Array.prototype.myEvery = function (callbackFn, thisArg) { const len = this.length; let flag = true; for (let i = 0; i item > 2)); // => false console.log([1, 2, 3].myEvery((item) => item > 0)); // => true </t>
Array.prototype.filter()
/** * @template T, U * @param { (value: T, index: number, array: Array<t>) => boolean } callbackFn * @param { any } [thisArg] * @return {Array<t>} */ Array.prototype.myFilter = function (callbackFn, thisArg) { const newArray = []; for (let i = 0; i value % 2 == 0)); // => [2, 4] console.log([1, 2, 3, 4].myFilter((value) => value [1, 2] </t></t>
Array.prototype.flat()
/** * @param { Array } arr * @param { number } depth * @returns { Array } */ function flatten(arr, depth = 1) { const newArray = []; for (let i = 0; i word.length > 6); console.log(result); // => ["exuberant", "destruction", "present"]
Array.prototype.forEach()
/** * @template T, U * @param { (value: T, index: number, array: Array<t>) => U } callbackFn * @param {any} [thisArg] * @return {Array<u>} */ Array.prototype.myForEach = function (callbackFn, thisArg) { if (this == null) { throw new TypeError("this is null or not defined"); } if (typeof callbackFn !== "function") { throw new TypeError(callbackFn + " is not a function"); } const O = Object(this); // Zero-fill Right Shift to ensure that the result if always non-negative. const len = O.length >>> 0; for (let i = 0; i el * el), null ); // => [1, 4, 9]; </u></t>
Array.prototype.indexOf()
/** * @param {any} searchElement * @param {number} fromIndex * @return {number} */ Array.prototype.myIndexOf = function (searchElement, fromIndex = 0) { const len = this.length; if (fromIndex 2 console.log([1, 2, 3, 4, 5].myIndexOf(6)); // => -1 console.log([1, 2, 3, 4, 5].myIndexOf(1)); // => 0 console.log(['a', 'b', 'c'].myIndexOf('b')); // => 1 console.log([NaN].myIndexOf(NaN)); // => -1 (since NaN !== NaN)
Array.prototype.last()
/** * @return {null|boolean|number|string|Array|Object} */ Array.prototype.myLast = function () { return this.length ? this.at(-1) : -1; }; // Usage example console.log([].myLast()); // => -1; console.log([1].myLast()); // => 1 console.log([1, 2].myLast()); // => 2
Array.prototype.map()
/** * @template T, U * @param { (value: T, index: number, array: Array<t>) => U } callbackFn * @param {any} [thisArg] * @return {Array<u>} */ Array.prototype.myMap = function (callbackFn, thisArg) { const len = this.length; const newArray = Array.from({ length: len }); for (let i = 0; i i)); // => [1, 2, 3, 4] console.log([1, 2, 3, 4].myMap((i) => i * i)); // => [1, 4, 9, 16]) </u></t>
Array.prototype.reduce()
/** * @template T, U * @param { (previousValue: U, currentValue: T, currentIndex: number, array: Array<t>) => U } callbackFn * @param {U} [initialValue] * @return {U} */ Array.prototype.myReduce = function (callbackFn, initialValue) { const hasInitialValue = initialValue !== undefined; const len = this.length; if (!hasInitialValue && !len) { throw new Error("Reduce of empty array with no initial value"); } let accumulator = hasInitialValue ? initialValue : this[0]; let startingIndex = hasInitialValue ? 0 : 1; for (let i = startingIndex; i acc + num, 0); console.log(sum); // => 15 const products = numbers.myReduce((acc, num) => acc * num, 1); </t>
Array.prototype.some()
/** * @template T * @param { (value: T, index: number, array: Array<t>) => boolean } callbackFn * @param {any} [thisArg] * @return {boolean} */ Array.prototype.mySome = function (callbackFn, thisArg) { const len = this.length; let flag = false; for (let i = 0; i item > 2)); // => true console.log([1, 2, 3].mySome((item) => item false </t>
Array.prototype.square()
/** * @return {Array<number>} */ Array.prototype.mySquare = function () { const len = this.length; const newArray = Array.from({ length: len }); for (let i = 0; i [1, 4, 9]; console.log([].mySquare()); // => []; </number>
參考
- 偉大的前端
- Array.prototype.at() - MDN
- Array.prototype.concat() - MDN
- Array.prototype.every() - MDN
- Array.prototype.filter() - MDN
- Array.prototype.flat() - MDN
- Array.prototype.forEach() - MDN
- Array.prototype.indexOf() - MDN
- Array.prototype.map() - MDN
- Array.prototype.reduce() - MDN
- Array.prototype.some() - MDN
- 2635。對數組中的每個元素套用變換 - LeetCode
- 2634。從陣列中過濾元素 - LeetCode
- 2626。數組歸約變換 - LeetCode
- 2619。最後的陣列原型 - LeetCode
- 2625。展平深度嵌套數組 - LeetCode
- 3.實作 Array.prototype.flat() - BFE.dev
- 151。實作 Array.prototype.map() - BFE.dev
- 146。實作 Array.prototype.reduce() - BFE.dev
以上是陣列原型 - JavaScript 挑戰的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1
好用且免費的程式碼編輯器