配列プロトタイプ - JavaScript の課題
この投稿のすべてのコードは、リポジトリ Github で見つけることができます。
アレイプロトタイプ関連の課題
Array.prototype.at()
/** * @param {number} index * @return {any | undefiend} */ Array.prototype.myAt = function (index) { const len = this.length; if (index < -len || 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]];
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 < len; i += 1) { if (Object.hasOwn(this, i) && !callbackFn.call(thisArg, this[i], i, this)) { flag = false; break; } } return flag; }; // Usage example console.log([1, 2, 3].myEvery((item) => item > 2)); // => false console.log([1, 2, 3].myEvery((item) => item > 0)); // => true
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 < this.length; i += 1) { if (Object.hasOwn(this, i) && callbackFn.call(thisArg, this[i], i, this)) { newArray.push(this[i]); } } return newArray; }; // Usage example console.log([1, 2, 3, 4].myFilter((value) => value % 2 == 0)); // => [2, 4] console.log([1, 2, 3, 4].myFilter((value) => value < 3)); // => [1, 2]
Array.prototype. flat()
/** * @param { Array } arr * @param { number } depth * @returns { Array } */ function flatten(arr, depth = 1) { const newArray = []; for (let i = 0; i < arr.length; i += 1) { if (Array.isArray(arr[i]) && depth !== 0) { newArray.push(...flatten(arr[i], depth - 1)); } else { newArray.push(arr[i]); } } return newArray; } // Usage example const words = ["spray", "elite", "exuberant", "destruction", "present"]; const result = words.filter((word) => 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 < len; i += 1) { if (Object.hasOwn(O, i)) { callbackFn.call(thisArg, O[i], i, O); } } }; // Usage example console.log( [1, 2, 3].myForEach((el) => el * el), null ); // => [1, 4, 9];
Array.prototype.indexOf()
/** * @param {any} searchElement * @param {number} fromIndex * @return {number} */ Array.prototype.myIndexOf = function (searchElement, fromIndex = 0) { const len = this.length; if (fromIndex < 0) { fromIndex = Math.max(0, fromIndex + this.length); } for (let i = fromIndex; i < len; i += 1) { if (this[i] === searchElement) { return i; } } return -1; } // Usage example console.log([1, 2, 3, 4, 5].myIndexOf(3)); // => 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 < len; i += 1) { if (Object.hasOwn(this, i)) { newArray[i] = callbackFn.call(thisArg, this[i], i, this); } } return newArray; }; // Usage example console.log([1, 2, 3, 4].myMap((i) => i)); // => [1, 2, 3, 4] console.log([1, 2, 3, 4].myMap((i) => i * i)); // => [1, 4, 9, 16])
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 < len; i += 1) { if (Object.hasOwn(this, i)) { accumulator = callbackFn(accumulator, this[i], i, this); } } return accumulator; }; // Usage example const numbers = [1, 2, 3, 4, 5]; const sum = numbers.myReduce((acc, num) => acc + num, 0); console.log(sum); // => 15 const products = numbers.myReduce((acc, num) => acc * num, 1);
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 < len; i += 1) { if (Object.hasOwn(this, i) && callbackFn.call(thisArg, this[i], i, this)) { flag = true; break; } } return flag; }; // Usage example console.log([1, 2, 3].mySome((item) => item > 2)); // => true console.log([1, 2, 3].mySome((item) => item < 0)); // => false
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 < len; i += 1) { newArray[i] = this[i] * this[i]; } return newArray; }; // Usage example console.log([1, 2, 3].mySquare()); // => [1, 4, 9]; console.log([].mySquare()); // => [];
参照
- 素晴らしいフロントエンド
- 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 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









記事では、JavaScriptライブラリの作成、公開、および維持について説明し、計画、開発、テスト、ドキュメント、およびプロモーション戦略に焦点を当てています。

この記事では、ブラウザでJavaScriptのパフォーマンスを最適化するための戦略について説明し、実行時間の短縮、ページの負荷速度への影響を最小限に抑えることに焦点を当てています。

フロントエンドのサーマルペーパーチケット印刷のためのよくある質問とソリューションフロントエンド開発におけるチケット印刷は、一般的な要件です。しかし、多くの開発者が実装しています...

この記事では、ブラウザ開発者ツールを使用した効果的なJavaScriptデバッグについて説明し、ブレークポイントの設定、コンソールの使用、パフォーマンスの分析に焦点を当てています。

スキルや業界のニーズに応じて、PythonおよびJavaScript開発者には絶対的な給与はありません。 1. Pythonは、データサイエンスと機械学習でさらに支払われる場合があります。 2。JavaScriptは、フロントエンドとフルスタックの開発に大きな需要があり、その給与もかなりです。 3。影響要因には、経験、地理的位置、会社の規模、特定のスキルが含まれます。

この記事では、ソースマップを使用して、元のコードにマッピングすることにより、Minified JavaScriptをデバッグする方法について説明します。ソースマップの有効化、ブレークポイントの設定、Chrome DevtoolsやWebpackなどのツールの使用について説明します。

同じIDを持つ配列要素をJavaScriptの1つのオブジェクトにマージする方法は?データを処理するとき、私たちはしばしば同じIDを持つ必要性に遭遇します...

Console.log出力の違いの根本原因に関する詳細な議論。この記事では、Console.log関数の出力結果の違いをコードの一部で分析し、その背後にある理由を説明します。 �...
