diff --git a/1-js/05-data-types/05-array-methods/1-camelcase/task.md b/1-js/05-data-types/05-array-methods/1-camelcase/task.md index ef5944636..77cec71a2 100644 --- a/1-js/05-data-types/05-array-methods/1-camelcase/task.md +++ b/1-js/05-data-types/05-array-methods/1-camelcase/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# Translate border-left-width to borderLeftWidth +# عبارت border-left-width را به borderLeftWidth تغییر دهید -Write the function `camelize(str)` that changes dash-separated words like "my-short-string" into camel-cased "myShortString". +تابع `camelize(str)` را بنویسید که کلمه‌های جدا شده توسط خط تیره مانند "my-short-string" را به عبارت camel-cased مانند "myShortString" تبدیل می‌کند. -That is: removes all dashes, each word after dash becomes uppercased. +یعنی اینکه تمام خط تیره‌ها را حذف کند و هر کلمه بعد از خط تیره با حرف بزرگ شروع شود. -Examples: +مثال‌ها: ```js camelize("background-color") == 'backgroundColor'; @@ -16,4 +16,4 @@ camelize("list-style-image") == 'listStyleImage'; camelize("-webkit-transition") == 'WebkitTransition'; ``` -P.S. Hint: use `split` to split the string into an array, transform it and `join` back. +پی‌نوشت راهنمایی: از `split` برای تبدیل رشته به آرایه استفاده کنید، آن را تغییر شکل دهید و با `join` آنها را پیوند بزنید. diff --git a/1-js/05-data-types/05-array-methods/10-average-age/task.md b/1-js/05-data-types/05-array-methods/10-average-age/task.md index bf5f85df3..a86b370d1 100644 --- a/1-js/05-data-types/05-array-methods/10-average-age/task.md +++ b/1-js/05-data-types/05-array-methods/10-average-age/task.md @@ -2,13 +2,13 @@ importance: 4 --- -# Get average age +# دریافت میانگین سن -Write the function `getAverageAge(users)` that gets an array of objects with property `age` and returns the average age. +تابع `getAverageAge(users)` بنویسید که آرایه‌ای از اشیای دارای ویژگی `age` می‌گیرد و میانگین سن را برمی‌گرداند. -The formula for the average is `(age1 + age2 + ... + ageN) / N`. +فرمول میانگین به این صورت است: `(age1 + age2 + ... + ageN) / N`. -For instance: +برای مثال: ```js no-beautify let john = { name: "John", age: 25 }; diff --git a/1-js/05-data-types/05-array-methods/11-array-unique/solution.md b/1-js/05-data-types/05-array-methods/11-array-unique/solution.md index 1adc8cd6c..82e15e88a 100644 --- a/1-js/05-data-types/05-array-methods/11-array-unique/solution.md +++ b/1-js/05-data-types/05-array-methods/11-array-unique/solution.md @@ -1,6 +1,6 @@ -Let's walk the array items: -- For each item we'll check if the resulting array already has that item. -- If it is so, then ignore, otherwise add to results. +بیایید المان‌های آرایه را بررسی کنیم: +- برای هر المان بررسی می‌کنیم که آیا آرایه حاصل دارای آن المان هست یا نه. +- اگر بود، المان را نادیده می‌گیریم، در غیر این صورت آن را به نتایج اضافه می‌کنیم. ```js run function unique(arr) { @@ -22,18 +22,18 @@ let strings = ["Hare", "Krishna", "Hare", "Krishna", alert( unique(strings) ); // Hare, Krishna, :-O ``` -The code works, but there's a potential performance problem in it. +این کد کار می‌کند، اما احتمالا یک اشکال عملکردی دارد. -The method `result.includes(str)` internally walks the array `result` and compares each element against `str` to find the match. +متد `result.includes(str)` درون آرایه `result` را بررسی می‌کند و هر المان را با `str` مقایسه می‌کند تا المان مورد نظر را پیدا کند. -So if there are `100` elements in `result` and no one matches `str`, then it will walk the whole `result` and do exactly `100` comparisons. And if `result` is large, like `10000`, then there would be `10000` comparisons. +ینابراین اگر `100` المان درون `result` وجود داشته باشد و هیچ کدام با `str` برابر نباشد، سپس تمام `result` را بررسی می‌کند و دقیقا `100` مقایسه انجام می‌دهد. و اگر `result` بزرگ باشد، مثلا `10000`، سپس به تعداد `10000` مقایسه وجود خواهد داشت. -That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds. +این به خودی خود مشکل محسوب نمی‌شود، چون موتورهای جاوااسکریپت بسیار سریع هستند، پس بررسی یک آرایه با `10000` المان چند میکروثانیه طول می‌کشد. -But we do such test for each element of `arr`, in the `for` loop. +اما ما در حلقه `for` چنین آزمایشی را برای هر المان درون `arr` انجام می‌دهیم. -So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot. +پس اگر `arr.length` برابر با `10000` باشد ما چیزی مثل `10000*10000` = 100 میلیون مقایسه خواهیم داشت. این مقدار بسیار زیاد است. -So the solution is only good for small arrays. +بنابراین این راه حل تنها برای آرایه‌های کوچک مناسب است. -Further in the chapter we'll see how to optimize it. +بعدا در فصل ما یاد می‌گیریم که چگونه آن را بهینه کنیم. diff --git a/1-js/05-data-types/05-array-methods/11-array-unique/task.md b/1-js/05-data-types/05-array-methods/11-array-unique/task.md index 5b56d3621..dc76c315e 100644 --- a/1-js/05-data-types/05-array-methods/11-array-unique/task.md +++ b/1-js/05-data-types/05-array-methods/11-array-unique/task.md @@ -2,17 +2,17 @@ importance: 4 --- -# Filter unique array members +# عضوهای یکتای آرایه را جدا کنید -Let `arr` be an array. +فرض کنیم که `arr` یک آرایه است. -Create a function `unique(arr)` that should return an array with unique items of `arr`. +تابع `unique(arr)` را بنویسید که آرایه‌ای شامل المان‌های یکتای `arr` را برمی‌گرداند. -For instance: +برای مثال: ```js function unique(arr) { - /* your code */ + /* کد شما */ } let strings = ["Hare", "Krishna", "Hare", "Krishna", diff --git a/1-js/05-data-types/05-array-methods/12-reduce-object/task.md b/1-js/05-data-types/05-array-methods/12-reduce-object/task.md index d3c8f8eb1..10ef44ede 100644 --- a/1-js/05-data-types/05-array-methods/12-reduce-object/task.md +++ b/1-js/05-data-types/05-array-methods/12-reduce-object/task.md @@ -2,13 +2,13 @@ importance: 4 --- -# Create keyed object from array +# شیءهای کلیددار از آرایه بسازید -Let's say we received an array of users in the form `{id:..., name:..., age... }`. +بیایید فرض کنیم که ما یک آرایه از کاربران به شکل `{id:..., name:..., age:... }` دریافت کرده‌ایم. -Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values. +یک تابع `groupById(arr)` بسازید که یک شیء از آن ایجاد می‌کند، که `id` به عنوان کلید و المان‌های آرایه به عنوان مقدار موجود هستند. -For example: +برای مثال: ```js let users = [ @@ -20,7 +20,7 @@ let users = [ let usersById = groupById(users); /* -// after the call we should have: +// بعد از فراخوانی ما باید این را داشته باشیم: usersById = { john: {id: 'john', name: "John Smith", age: 20}, @@ -30,8 +30,8 @@ usersById = { */ ``` -Such function is really handy when working with server data. +چنین تابعی هنگام کار کردن با داده سرور خیلی به کار می‌آید. -In this task we assume that `id` is unique. There may be no two array items with the same `id`. +در این تکلیف ما فرض می‌کنیم که `id` یکتا است. هیچ دو المانی از آرایه وجود ندارد که `id` یکسانی داشته باشند. -Please use array `.reduce` method in the solution. +لطفا از متد `.reduce` در راه حل استفاده کنید. diff --git a/1-js/05-data-types/05-array-methods/2-filter-range/solution.md b/1-js/05-data-types/05-array-methods/2-filter-range/solution.md index c1a4ac3cb..bee08e408 100644 --- a/1-js/05-data-types/05-array-methods/2-filter-range/solution.md +++ b/1-js/05-data-types/05-array-methods/2-filter-range/solution.md @@ -1,6 +1,6 @@ ``` js run function filterRange(arr, a, b) { - // added brackets around the expression for better readability + // برای خوانایی بهتر به دور عبارت، پرانتز اضافه کردیم return arr.filter(item => (a <= item && item <= b)); } @@ -8,7 +8,7 @@ let arr = [5, 3, 8, 1]; let filtered = filterRange(arr, 1, 4); -alert( filtered ); // 3,1 (matching values) +alert( filtered ); // 3,1 (مقدارهای مورد نظر) -alert( arr ); // 5,3,8,1 (not modified) +alert( arr ); // 5,3,8,1 (تغییر داده نشده) ``` diff --git a/1-js/05-data-types/05-array-methods/2-filter-range/task.md b/1-js/05-data-types/05-array-methods/2-filter-range/task.md index 46e47c93d..5a748b5da 100644 --- a/1-js/05-data-types/05-array-methods/2-filter-range/task.md +++ b/1-js/05-data-types/05-array-methods/2-filter-range/task.md @@ -2,21 +2,21 @@ importance: 4 --- -# Filter range +# جداسازی توسط حدود -Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements with values higher or equal to `a` and lower or equal to `b` and return a result as an array. +یک تابع `filterRange(arr, a, b)` بنویسید که یک آرایه `arr` دریافت می‌کند، به دنبال المان‌های بزرگتر یا مساوی `a` و کوچکتر یا مساوی `b` می‌گردد و نتیجه را به شکل آرایه برمی‌گرداند. -The function should not modify the array. It should return the new array. +تابع نباید آرایه داده شده را تغییر دهد. باید آرایه جدید را برگرداند. -For instance: +برای مثال: ```js let arr = [5, 3, 8, 1]; let filtered = filterRange(arr, 1, 4); -alert( filtered ); // 3,1 (matching values) +alert( filtered ); // 3,1 (مقدارهای مورد نظر) -alert( arr ); // 5,3,8,1 (not modified) +alert( arr ); // 5,3,8,1 (تغییر داده نشده) ``` diff --git a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md index 53de3a57f..eb7b289fd 100644 --- a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md +++ b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md @@ -4,7 +4,7 @@ function filterRangeInPlace(arr, a, b) { for (let i = 0; i < arr.length; i++) { let val = arr[i]; - // remove if outside of the interval + // اگر بیرون از بازه بود آن را حذف کن if (val < a || val > b) { arr.splice(i, 1); i--; @@ -15,7 +15,7 @@ function filterRangeInPlace(arr, a, b) { let arr = [5, 3, 8, 1]; -filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4 +filterRangeInPlace(arr, 1, 4); // تمام اعداد به جز 1 تا 4 را حذف کرد alert( arr ); // [3, 1] diff --git a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md index 7066a51ab..09df50f43 100644 --- a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md +++ b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md @@ -2,17 +2,17 @@ importance: 4 --- -# Filter range "in place" +# جداسازی توسط حدود «در محل» -Write a function `filterRangeInPlace(arr, a, b)` that gets an array `arr` and removes from it all values except those that are between `a` and `b`. The test is: `a ≤ arr[i] ≤ b`. +تابع `filterRangeInPlace(arr, a, b)` را بنویسید که یک آرایه `arr` دریافت می‌کند و تمام مقدارها به جز آنهایی که بین `a` و `b` هستند را حذف می‌کند. آزمایش به این صورت است: `a ≤ arr[i] ≤ b`. -The function should only modify the array. It should not return anything. +تابع باید فقط آرایه را تغییر دهد. نباید چیزی را برگرداند. -For instance: +برای مثال: ```js let arr = [5, 3, 8, 1]; -filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4 +filterRangeInPlace(arr, 1, 4); // تمام اعداد به جز 1 تا 4 را حذف کردیم alert( arr ); // [3, 1] ``` diff --git a/1-js/05-data-types/05-array-methods/4-sort-back/task.md b/1-js/05-data-types/05-array-methods/4-sort-back/task.md index 0e3eeab76..542ba1aec 100644 --- a/1-js/05-data-types/05-array-methods/4-sort-back/task.md +++ b/1-js/05-data-types/05-array-methods/4-sort-back/task.md @@ -2,12 +2,12 @@ importance: 4 --- -# Sort in decreasing order +# مرتب‌سازی با ترتیب نزولی ```js let arr = [5, 2, 1, -10, 8]; -// ... your code to sort it in decreasing order +// ... کد شما برای مرتب کردن آرایه با ترتیب نزولی alert( arr ); // 8, 5, 2, 1, -10 ``` diff --git a/1-js/05-data-types/05-array-methods/5-copy-sort-array/solution.md b/1-js/05-data-types/05-array-methods/5-copy-sort-array/solution.md index 8537b129e..2cb0becf9 100644 --- a/1-js/05-data-types/05-array-methods/5-copy-sort-array/solution.md +++ b/1-js/05-data-types/05-array-methods/5-copy-sort-array/solution.md @@ -1,4 +1,4 @@ -We can use `slice()` to make a copy and run the sort on it: +برای ایجاد یک کپی و اجرای مرتب‌سازی روی آن، می‌توانیم از `slice()` استفاده کنیم: ```js run function copySorted(arr) { diff --git a/1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md b/1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md index c1395b4ad..f74c75334 100644 --- a/1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md +++ b/1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md @@ -2,11 +2,11 @@ importance: 5 --- -# Copy and sort array +# کپی و مرتب کردن آرایه -We have an array of strings `arr`. We'd like to have a sorted copy of it, but keep `arr` unmodified. +ما آرایه‌ای از رشته‌ها `arr` داریم. ما می‌خواهیم یک کپی مرتب شده از آن را داشته باشیم اما `arr` را تغییر ندهیم. -Create a function `copySorted(arr)` that returns such a copy. +یک تابع `copySorted(arr)` بسازید که چنین کپی‌ای را برگرداند. ```js let arr = ["HTML", "JavaScript", "CSS"]; @@ -14,5 +14,5 @@ let arr = ["HTML", "JavaScript", "CSS"]; let sorted = copySorted(arr); alert( sorted ); // CSS, HTML, JavaScript -alert( arr ); // HTML, JavaScript, CSS (no changes) +alert( arr ); // HTML, JavaScript, CSS (بدون تغییر) ``` diff --git a/1-js/05-data-types/05-array-methods/6-array-get-names/task.md b/1-js/05-data-types/05-array-methods/6-array-get-names/task.md index 74c8a9d74..34f924d77 100644 --- a/1-js/05-data-types/05-array-methods/6-array-get-names/task.md +++ b/1-js/05-data-types/05-array-methods/6-array-get-names/task.md @@ -2,11 +2,11 @@ importance: 5 --- -# Map to names +# اسم‌ها را دریابید -You have an array of `user` objects, each one has `user.name`. Write the code that converts it into an array of names. +شما یک آرایه از شیءهای `user` دارید که هر کدام دارای `user.name` هستند. کدی بنویسید که آن را به آرایه‌ای از اسم‌ها تبدیل می‌کند. -For instance: +برای مثال: ```js no-beautify let john = { name: "John", age: 25 }; @@ -15,7 +15,7 @@ let mary = { name: "Mary", age: 28 }; let users = [ john, pete, mary ]; -let names = /* ... your code */ +let names = /* ... کد شما */ alert( names ); // John, Pete, Mary ``` diff --git a/1-js/05-data-types/05-array-methods/6-calculator-extendable/solution.md b/1-js/05-data-types/05-array-methods/6-calculator-extendable/solution.md index ebe0714cf..bc97539f0 100644 --- a/1-js/05-data-types/05-array-methods/6-calculator-extendable/solution.md +++ b/1-js/05-data-types/05-array-methods/6-calculator-extendable/solution.md @@ -1,3 +1,3 @@ -- Please note how methods are stored. They are simply added to `this.methods` property. -- All tests and numeric conversions are done in the `calculate` method. In future it may be extended to support more complex expressions. +- لطفا در نظر داشته باشید که متدها چگونه ذخیره شده‌اند. آنها به سادگی به ویژگی `this.methods` اضافه شده‌اند. +- تمام آزمایش‌ها و تبدیلات عددی در متد `calculate` انجام می‌شوند. در آینده ممکن است برای پشتیبانی از عبارات پیچیده‌تر توسعه بیابد. diff --git a/1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md b/1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md index e0d302f4c..1c3c31c82 100644 --- a/1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md +++ b/1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md @@ -2,24 +2,24 @@ importance: 5 --- -# Create an extendable calculator +# یک ماشین حساب قابل توسعه بسازید -Create a constructor function `Calculator` that creates "extendable" calculator objects. +یک تابع سازنده `Calculator` بسازید که شیءهای ماشین حساب «قابل توسعه» می‌سازد. -The task consists of two parts. +این تکلیف از دو بخش تشکیل شده است. -1. First, implement the method `calculate(str)` that takes a string like `"1 + 2"` in the format "NUMBER operator NUMBER" (space-delimited) and returns the result. Should understand plus `+` and minus `-`. +1. اول، متد `calculate(str)` را بسازید که یک رشته مانند `"1 + 2"` را در شکل «عدد عملگر عدد» دریافت می‌کند و نتیجه را برمی‌گرداند. این متد باید جمع `+` و منها `-` را متوجه شود. - Usage example: + مثالی از کاربرد آن: ```js let calc = new Calculator; alert( calc.calculate("3 + 7") ); // 10 ``` -2. Then add the method `addMethod(name, func)` that teaches the calculator a new operation. It takes the operator `name` and the two-argument function `func(a,b)` that implements it. +2. سپس متد `addMethod(name, func)` را اضافه کنید که به ماشین حساب یک عملیات جدید را آموزش می‌دهد. این متد اسم عملگر `name` و تابع دو آرگومانی `func(a,b)` که عملیات را پیاده‌سازی می‌کند را دریافت می‌کند. - For instance, let's add the multiplication `*`, division `/` and power `**`: + برای مثال، بیایید عمل ضرب `*`، تقسیم `/` و به توان رساندن `**` را اضافه کنیم: ```js let powerCalc = new Calculator; @@ -31,6 +31,6 @@ The task consists of two parts. alert( result ); // 8 ``` -- No parentheses or complex expressions in this task. -- The numbers and the operator are delimited with exactly one space. -- There may be error handling if you'd like to add it. +- پرانتز یا عبارات پیچیده در این تکلیف وجود ندارند. +- اعداد و عملگر دقیقا به یک فاصله خالی محدود می‌شوند. +- اگر دوست داشته باشید می‌توانید مدیریت ارور را هم اضافه کنید. diff --git a/1-js/05-data-types/05-array-methods/7-map-objects/solution.md b/1-js/05-data-types/05-array-methods/7-map-objects/solution.md index 2d8d4fb0e..295eb2537 100644 --- a/1-js/05-data-types/05-array-methods/7-map-objects/solution.md +++ b/1-js/05-data-types/05-array-methods/7-map-objects/solution.md @@ -25,9 +25,9 @@ alert( usersMapped[0].id ); // 1 alert( usersMapped[0].fullName ); // John Smith ``` -Please note that in the arrow functions we need to use additional brackets. +لطفا در نظر داشته باشید که در تابع‌های کمانی ما باید از پرانتزهای اضافی استفاده کنیم. -We can't write like this: +نمی‌توانیم اینگونه بنویسیم: ```js let usersMapped = users.map(user => *!*{*/!* fullName: `${user.name} ${user.surname}`, @@ -35,9 +35,9 @@ let usersMapped = users.map(user => *!*{*/!* }); ``` -As we remember, there are two arrow functions: without body `value => expr` and with body `value => {...}`. +همانطور که به یاد داریم، دو نوع تابع کمانی وجود دارد: بدون بدنه `value => expr` و همراه با بدنه `value => {...}`. -Here JavaScript would treat `{` as the start of function body, not the start of the object. The workaround is to wrap them in the "normal" brackets: +اینجا جاوااسکریپت با `}` به عنوان آغاز بدنه تابع رفتار می‌کند نه آغاز شیء. راه حل در پیچیدن آنها درون یک «پرانتر» است: ```js let usersMapped = users.map(user => *!*({*/!* @@ -46,6 +46,6 @@ let usersMapped = users.map(user => *!*({*/!* })); ``` -Now fine. +حالا درست است. diff --git a/1-js/05-data-types/05-array-methods/7-map-objects/task.md b/1-js/05-data-types/05-array-methods/7-map-objects/task.md index b11d12155..cc272831e 100644 --- a/1-js/05-data-types/05-array-methods/7-map-objects/task.md +++ b/1-js/05-data-types/05-array-methods/7-map-objects/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# Map to objects +# دست‌یابی به شیءها -You have an array of `user` objects, each one has `name`, `surname` and `id`. +شما آرایه‌ای از شیءهای `user` دارید که هر کدام دارای `name`، `surname` و `id` هستند. -Write the code to create another array from it, of objects with `id` and `fullName`, where `fullName` is generated from `name` and `surname`. +کدی برای ساختن یک آرایه دیگر از آن بنویسید که شامل شیءهای دارای `id` و `fullName` است، که `fullName` از `name` و `suname` ایجاد می‌شود. -For instance: +برای مثال: ```js no-beautify let john = { name: "John", surname: "Smith", id: 1 }; @@ -18,7 +18,7 @@ let mary = { name: "Mary", surname: "Key", id: 3 }; let users = [ john, pete, mary ]; *!* -let usersMapped = /* ... your code ... */ +let usersMapped = /* ... کد شما ... */ */!* /* @@ -33,4 +33,4 @@ alert( usersMapped[0].id ) // 1 alert( usersMapped[0].fullName ) // John Smith ``` -So, actually you need to map one array of objects to another. Try using `=>` here. There's a small catch. \ No newline at end of file +پس در واقع شما باید طرحی از آرایه‌ای از شیءها برای آرایه‌ای دیگر بیابید. سعی کنید از `<=` اینجا استفاده کنید. یک فریب کوچک وجود دارد. diff --git a/1-js/05-data-types/05-array-methods/8-sort-objects/solution.md b/1-js/05-data-types/05-array-methods/8-sort-objects/solution.md index 370aea78d..85be5d4a9 100644 --- a/1-js/05-data-types/05-array-methods/8-sort-objects/solution.md +++ b/1-js/05-data-types/05-array-methods/8-sort-objects/solution.md @@ -11,7 +11,7 @@ let arr = [ john, pete, mary ]; sortByName(arr); -// now sorted is: [john, mary, pete] +// [john, mary, pete] :حالا آرایه مرتب شده به این شکل است alert(arr[0].name); // John alert(arr[2].name); // Pete ``` diff --git a/1-js/05-data-types/05-array-methods/8-sort-objects/task.md b/1-js/05-data-types/05-array-methods/8-sort-objects/task.md index 498cf5bc2..652a96a01 100644 --- a/1-js/05-data-types/05-array-methods/8-sort-objects/task.md +++ b/1-js/05-data-types/05-array-methods/8-sort-objects/task.md @@ -2,11 +2,11 @@ importance: 5 --- -# Sort objects +# مرتب‌سازی شیءها -Write the function `sortByName(users)` that gets an array of objects with the `age` property and sorts them by `age`. +تابع `sortByName(users)` را بنویسید که آرایه‌ای از شیءهای شامل `age` دریافت می‌کند و آنها را بر اساس `age` مرتب می‌کند. -For instance: +برای مثال: ```js no-beautify let john = { name: "John", age: 25 }; diff --git a/1-js/05-data-types/05-array-methods/9-shuffle/solution.md b/1-js/05-data-types/05-array-methods/9-shuffle/solution.md index 6674c444f..ff721d50f 100644 --- a/1-js/05-data-types/05-array-methods/9-shuffle/solution.md +++ b/1-js/05-data-types/05-array-methods/9-shuffle/solution.md @@ -1,4 +1,4 @@ -The simple solution could be: +راه حل ساده می‌تواند اینگونه باشد: ```js run *!* @@ -12,18 +12,18 @@ shuffle(arr); alert(arr); ``` -That somewhat works, because `Math.random() - 0.5` is a random number that may be positive or negative, so the sorting function reorders elements randomly. +این کد تا حدی کار می‌کند، چون `Math.random() - 0.5` یک عدد تصادفی است که ممکن است مثبت یا منفی باشد، پس تابع مرتب‌کننده المان‌ها را به صورت تصادفی مرتب می‌کند. -But because the sorting function is not meant to be used this way, not all permutations have the same probability. +اما به دلیل اینکه تابع مرتب‌کننده قرار نیست اینگونه استفاده شود، تمام جابجایی‌ها احتمال برابر ندارند. -For instance, consider the code below. It runs `shuffle` 1000000 times and counts appearances of all possible results: +برای مثال، کد زیر را در نظر بگیرید. این کد `shuffle` را 1000000 بار اجرا می‌کند و تعداد وقوع تمام نتایج ممکن را می‌شمارد: ```js run function shuffle(array) { array.sort(() => Math.random() - 0.5); } -// counts of appearances for all possible permutations +// شمارش تعداد وقوع تمام جایجایی‌های ممکن let count = { '123': 0, '132': 0, @@ -39,13 +39,13 @@ for (let i = 0; i < 1000000; i++) { count[array.join('')]++; } -// show counts of all possible permutations +// نمایش شمارش تمام جابجایی‌های ممکن for (let key in count) { alert(`${key}: ${count[key]}`); } ``` -An example result (depends on JS engine): +نتیجه یک مثال (با وابستگی به موتور جاوااسکریپت): ```js 123: 250706 @@ -56,30 +56,30 @@ An example result (depends on JS engine): 321: 125223 ``` -We can see the bias clearly: `123` and `213` appear much more often than others. +می‌توانیم جانب‌داری را به طور واضح ببینیم: `123` و `213` نسبت به بقیه بیشتر رخ می‌دهند. -The result of the code may vary between JavaScript engines, but we can already see that the approach is unreliable. +نتیجه کد ممکن است بین موتورهای جاوااسکریپت متفاوت باشد، اما می‌توانیم به سادگی ببینیم که این شیوه قابل اطمینان نیست. -Why it doesn't work? Generally speaking, `sort` is a "black box": we throw an array and a comparison function into it and expect the array to be sorted. But due to the utter randomness of the comparison the black box goes mad, and how exactly it goes mad depends on the concrete implementation that differs between engines. +چرا درست کار نمی‌کند؟ به طور کلی، `sort` یک «جعبه سیاه» است: ما یک آرایه و تابع مقایسه را درون آن می‌اندازیم و توقع داریم که آرایه مرتب شود. اما به دلیل منطق تصادفی مقایسه، جعبه سیاه دیوانه می‌شود و اینکه چگونه دیوانه می‌شود بستگی به پیاده‌سازی دارد که بین موتورها متفاوت است. -There are other good ways to do the task. For instance, there's a great algorithm called [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). The idea is to walk the array in the reverse order and swap each element with a random one before it: +راه‌های خوب دیگری هم برای انجام تکلیف وجود دارد. برای مثال، یک الگوریتم عالی به اسم [بُرزدن Fisher-Yates](https://fa.wikipedia.org/wiki/برزدن_فیشر_یتس) وجود دارد. ایده اینگونه است که آرایه را برعکس بررسی کنیم و هر المان را با یک المان تصادفی قبل خود جابجا کنیم: ```js function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { - let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i + let j = Math.floor(Math.random() * (i + 1)); // i ایندکس تصادفی بین 0 تا - // swap elements array[i] and array[j] - // we use "destructuring assignment" syntax to achieve that - // you'll find more details about that syntax in later chapters - // same can be written as: + // را جابجا کن array[j] و array[i] المان‌های + // ما از سینتکس «مقداردهی تخریب‌ساختار» برای انجام آن استفاده می‌کنیم + // شما درباره سینتکس آن در فصل‌های آینده بیشتر یاد می‌گیرید + // به طور یکسان می‌توان نوشت: // let t = array[i]; array[i] = array[j]; array[j] = t [array[i], array[j]] = [array[j], array[i]]; } } ``` -Let's test it the same way: +بیایید مانند قبل آن را آزمایش کنیم: ```js run function shuffle(array) { @@ -89,7 +89,7 @@ function shuffle(array) { } } -// counts of appearances for all possible permutations +// شمارش تعداد وقوع تمام جایجایی‌های ممکن let count = { '123': 0, '132': 0, @@ -105,13 +105,13 @@ for (let i = 0; i < 1000000; i++) { count[array.join('')]++; } -// show counts of all possible permutations +// نمایش شمارش تمام جابجایی‌های ممکن for (let key in count) { alert(`${key}: ${count[key]}`); } ``` -The example output: +خروجی نمونه: ```js 123: 166693 @@ -122,6 +122,6 @@ The example output: 321: 166316 ``` -Looks good now: all permutations appear with the same probability. +الان بنظر خوب می‌آید: تمام جایجایی‌ها با احتمال یکسان رخ می‌دهند. -Also, performance-wise the Fisher-Yates algorithm is much better, there's no "sorting" overhead. +همچنین، الگوریتم Fisher-Yates که از نظر عملکرد بهینه می‌باشد، بسیار بهتر است، و هیچ مرتب‌سازی‌ای بر روی آن وجود ندارد. diff --git a/1-js/05-data-types/05-array-methods/9-shuffle/task.md b/1-js/05-data-types/05-array-methods/9-shuffle/task.md index 970c53417..412a57880 100644 --- a/1-js/05-data-types/05-array-methods/9-shuffle/task.md +++ b/1-js/05-data-types/05-array-methods/9-shuffle/task.md @@ -2,11 +2,11 @@ importance: 3 --- -# Shuffle an array +# آرایه را بُر بزنید -Write the function `shuffle(array)` that shuffles (randomly reorders) elements of the array. +تابع `shuffle(array)` را بنویسید که المان‌های آرایه را بُر می‌زند (به طور تصادفی ترتیب‌بندی می‌کند). -Multiple runs of `shuffle` may lead to different orders of elements. For instance: +هر بار فراخوانی `shuffle` ممکن است به ترتیب متفاوتی از المان‌ها منجر شود. برای مثال: ```js let arr = [1, 2, 3]; @@ -22,4 +22,4 @@ shuffle(arr); // ... ``` -All element orders should have an equal probability. For instance, `[1,2,3]` can be reordered as `[1,2,3]` or `[1,3,2]` or `[3,1,2]` etc, with equal probability of each case. +ترتیب تمام المان‌ها باید احتمال برابر داشته باشند. برای مثال، `[1,2,3]` می‌تواند به شکل `[1,2,3]` یا `[1,3,2]` یا `[3,1,2]` و... مرتب شود که احتمال هر مورد برابر است. diff --git a/1-js/05-data-types/05-array-methods/article.md b/1-js/05-data-types/05-array-methods/article.md index b14e9a0be..78189947f 100644 --- a/1-js/05-data-types/05-array-methods/article.md +++ b/1-js/05-data-types/05-array-methods/article.md @@ -1,28 +1,28 @@ -# Array methods +# متدهای آرایه -Arrays provide a lot of methods. To make things easier, in this chapter they are split into groups. +آرایه‌ها متدهای زیادی را فراهم می‌کنند. برای ساده‌سازی، در این فصل متدها به چند گروه تقسیم شده‌اند. -## Add/remove items +## اضافه/حذف کردن عضوها -We already know methods that add and remove items from the beginning or the end: +ما از قبل متدهایی که در آغاز یا انتهای آرایه چیزی را حذف یا اضافه می‌کنند را می‌شناسیم: -- `arr.push(...items)` -- adds items to the end, -- `arr.pop()` -- extracts an item from the end, -- `arr.shift()` -- extracts an item from the beginning, -- `arr.unshift(...items)` -- adds items to the beginning. +- `arr.push(...items)` -- المان‌ها را به انتها اضافه می‌کند، +- `arr.pop()` -- یک المان را از انتها خارج می‌کند، +- `arr.shift()` -- یک المان را از آغاز خارج می‌کند، +- `arr.unshift(...items)` -- یک المان را به آغاز اضافه می‌کند. -Here are a few others. +اینجا چند متد دیگر داریم. -### splice +### متد splice -How to delete an element from the array? +چطور یک المان را از آرایه حذف کنیم؟ -The arrays are objects, so we can try to use `delete`: +آرایه‌ها شیء هستند، پس می‌توانیم از `delete` استفاده کنیم: ```js run let arr = ["I", "go", "home"]; -delete arr[1]; // remove "go" +delete arr[1]; // "go" حذف alert( arr[1] ); // undefined @@ -30,146 +30,146 @@ alert( arr[1] ); // undefined alert( arr.length ); // 3 ``` -The element was removed, but the array still has 3 elements, we can see that `arr.length == 3`. +المان حذف شد، اما آرایه هنوز هم 3 عضو دارد که می‌توانیم آن را با `arr.length == 3` ببینیم. -That's natural, because `delete obj.key` removes a value by the `key`. It's all it does. Fine for objects. But for arrays we usually want the rest of elements to shift and occupy the freed place. We expect to have a shorter array now. +این چیز طبیعی است چون `delete obj.key` یک مقدار را با استفاده از `key` حذف می‌کند. به طور کلی کارش همین است. برای شیءها مناسب است. اما برای آرایه‌ها ما معمولا می‌خواهیم که بقیه المان‌ها پخش شوند و فضای آزاد شده را اشغال کنند. توقع داریم که الان آرایه‌ای کوتاه‌تر داشته باشیم. -So, special methods should be used. +بنابراین متدهای خاص باید استفاده شوند. -The [arr.splice](mdn:js/Array/splice) method is a swiss army knife for arrays. It can do everything: insert, remove and replace elements. +متد [arr.splice](mdn:js/Array/splice) یک شمشیر ارتشی سوئیسی برای آرایه‌ها است. می‌تواند هر کاری کند: اضافه کند، حذف کند و المان‌ها را جایگزین کند. -The syntax is: +سینتکس آن اینگونه است: ```js arr.splice(start[, deleteCount, elem1, ..., elemN]) ``` -It modifies `arr` starting from the index `start`: removes `deleteCount` elements and then inserts `elem1, ..., elemN` at their place. Returns the array of removed elements. +این متد `arr` را از ایندکس `start` تغییر می‌دهد: به تعداد `deleteCount` المان حذف می‌کند و سپس `elem1, ..., elemN` را در مکان خودشان اضافه می‌کند. آرایه‌ای از المان‌های حذف شده را برمی‌گرداند. -This method is easy to grasp by examples. +این متد را با مثال به راحتی متوجه می‌شوید. -Let's start with the deletion: +بیایید با حذف کردن شروع کنیم: ```js run let arr = ["I", "study", "JavaScript"]; *!* -arr.splice(1, 1); // from index 1 remove 1 element +arr.splice(1, 1); // از ایندکس 1 به تعداد 1 المان حذف کن */!* alert( arr ); // ["I", "JavaScript"] ``` -Easy, right? Starting from the index `1` it removed `1` element. +راحت است، نه؟ از ایندکس `1` به تعداد `1` المان حذف کرد. -In the next example we remove 3 elements and replace them with the other two: +در مثال بعد ما 3 المان را حذف و آنها را با دو المان جایگزین می‌کنیم: ```js run let arr = [*!*"I", "study", "JavaScript",*/!* "right", "now"]; -// remove 3 first elements and replace them with another +// سه المان ابتدایی را حذف کن و آنها را با المان‌های دیگر جایگزین کن arr.splice(0, 3, "Let's", "dance"); alert( arr ) // now [*!*"Let's", "dance"*/!*, "right", "now"] ``` -Here we can see that `splice` returns the array of removed elements: +اینجا می‌بینیم که `splice` آرایه‌ای از المان‌های حذف شده را برمی‌گرداند: ```js run let arr = [*!*"I", "study",*/!* "JavaScript", "right", "now"]; -// remove 2 first elements +// دو المان اول را حذف کن let removed = arr.splice(0, 2); alert( removed ); // "I", "study" <-- array of removed elements ``` -The `splice` method is also able to insert the elements without any removals. For that we need to set `deleteCount` to `0`: +متد `splice` همچنین قادر به اضافه کردن المان بدون هیچ حذفیاتی است. برای این کار باید `deleteCount` را `0` بگذاریم: ```js run let arr = ["I", "study", "JavaScript"]; -// from index 2 -// delete 0 -// then insert "complex" and "language" +// از ایندکس 2 +// به تعداد 0 حذف کن +// را اضافه کن "language" و "complex" سپس arr.splice(2, 0, "complex", "language"); alert( arr ); // "I", "study", "complex", "language", "JavaScript" ``` -````smart header="Negative indexes allowed" -Here and in other array methods, negative indexes are allowed. They specify the position from the end of the array, like here: +````smart header="ایندکس‌های منفی مجاز هستند" +اینجا و در دیگر متدهای آرایه، ایندکس‌های منفی قابل استفاده هستند. آنها موقعیت را از انتهای آرایه مشخص می‌کنند، مثل اینجا: ```js run let arr = [1, 2, 5]; -// from index -1 (one step from the end) -// delete 0 elements, -// then insert 3 and 4 +// از ایندکس 1- (یک قدم قبل از انتها) +// به تعداد 0 المان حذف کن، +// سپس 3 و 4 را اضافه کن arr.splice(-1, 0, 3, 4); alert( arr ); // 1,2,3,4,5 ``` ```` -### slice +### متد slice -The method [arr.slice](mdn:js/Array/slice) is much simpler than similar-looking `arr.splice`. +متد [arr.slice](mdn:js/Array/slice) از متد `arr.splice` که از لحاظ ظاهری شبیه به آن است بسیار ساده‌تر است. -The syntax is: +سینتکس اینگونه است: ```js arr.slice([start], [end]) ``` -It returns a new array copying to it all items from index `start` to `end` (not including `end`). Both `start` and `end` can be negative, in that case position from array end is assumed. +این متد یک آرایه جدید که تمام المان‌ها را از ایندکس `start` تا `end` (شامل خود `end` نمی‌شود) کپی می‌کند، برمی‌گرداند. `start` و `end` هر دو می‌توانند منفی باشند، که در این صورت موقعیت از انتهای آرایه حساب می‌شود. -It's similar to a string method `str.slice`, but instead of substrings it makes subarrays. +این متد شبیه متد رشته `str.slice` است، اما به جای زیر رشته، زیر آرایه ایجاد می‌کند. -For instance: +برای مثال: ```js run let arr = ["t", "e", "s", "t"]; -alert( arr.slice(1, 3) ); // e,s (copy from 1 to 3) +alert( arr.slice(1, 3) ); // e,s (کپی کردن از 1 تا 3) -alert( arr.slice(-2) ); // s,t (copy from -2 till the end) +alert( arr.slice(-2) ); // s,t (کپی کردن از 2- تا انتها) ``` -We can also call it without arguments: `arr.slice()` creates a copy of `arr`. That's often used to obtain a copy for further transformations that should not affect the original array. +همچنین می‌توانیم آن را بدون آرگومان هم صدا بزنیم: `arr.slice()` که یک کپی از `arr` می‌سازد. معمولا از این روش برای ایجاد یک کپی با هدف اینکه تغییرات آینده روی آرایه اصلی تاثیری نگذارد استفاده می‌کنند. -### concat +### متد concat -The method [arr.concat](mdn:js/Array/concat) creates a new array that includes values from other arrays and additional items. +متد [arr.concat](mdn:js/Array/concat) یک آرایه جدید می‌سازد که حاوی مقدارهای آرایه‌های دیگر و المان‌های اضافی است. -The syntax is: +سینتکس آن اینگونه است: ```js arr.concat(arg1, arg2...) ``` -It accepts any number of arguments -- either arrays or values. +این متد به هر تعدادی آرگومان می‌پذیرد -- چه آرایه باشند چه مقدار. -The result is a new array containing items from `arr`, then `arg1`, `arg2` etc. +نتیجه آن یک آرایه جدید حاوی المان‌های `arr`، سپس `arg1`، `arg2` و غیره. -If an argument `argN` is an array, then all its elements are copied. Otherwise, the argument itself is copied. +اگر آرگومان `argN` یک آرایه باشد، سپس تمام المان‌های آن کپی می‌شود. در غیر این صورت، خود آرگومان کپی می‌شود. -For instance: +برای مثال: ```js run let arr = [1, 2]; -// create an array from: arr and [3,4] +// و [3,4] arr :ساخت یک آرایه از alert( arr.concat([3, 4]) ); // 1,2,3,4 -// create an array from: arr and [3,4] and [5,6] +// و [3,4] و [5,6] arr :ساخت یک آرایه از alert( arr.concat([3, 4], [5, 6]) ); // 1,2,3,4,5,6 -// create an array from: arr and [3,4], then add values 5 and 6 +// و [3,4]، سپس اضافه کردن مقدارهای 5 و 6 arr :ساخت یک آرایه از alert( arr.concat([3, 4], 5, 6) ); // 1,2,3,4,5,6 ``` -Normally, it only copies elements from arrays. Other objects, even if they look like arrays, are added as a whole: +به طور معمول، این متد فقط المان‌ها را از آرایه‌ها کپی می‌کند. بقیه شیءها، حتی اگر شبیه آرایه باشند، به طور کلی اضافه می‌شوند: ```js run let arr = [1, 2]; @@ -182,7 +182,7 @@ let arrayLike = { alert( arr.concat(arrayLike) ); // 1,2,[object Object] ``` -...But if an array-like object has a special `Symbol.isConcatSpreadable` property, then it's treated as an array by `concat`: its elements are added instead: +...اما اگر یک شیء شبیه به آرایه یک ویژگی `Symbol.isConcatSpreadable` داشته باشد، سپس `concat` با آن به عنوان یک آرایه رفتار می‌کند: در عوض المان‌های آن اضافه می‌شوند: ```js run let arr = [1, 2]; @@ -199,25 +199,25 @@ let arrayLike = { alert( arr.concat(arrayLike) ); // 1,2,something,else ``` -## Iterate: forEach +## حلقه زدن: forEach -The [arr.forEach](mdn:js/Array/forEach) method allows to run a function for every element of the array. +متد [arr.forEach](mdn:js/Array/forEach) به ما این امکان را می‌دهد که یک تابع را روی تمام المان‌های آرایه اجرا کنیم. -The syntax: +سینتکس اینگونه است: ```js arr.forEach(function(item, index, array) { - // ... do something with item + // ... با المان کاری انجام دهید }); ``` -For instance, this shows each element of the array: +برای مثال، این کد هر المان آرایه را نشان می‌دهد: ```js run -// for each element call alert +// را صدا بزن alert برای هر المان ["Bilbo", "Gandalf", "Nazgul"].forEach(alert); ``` -And this code is more elaborate about their positions in the target array: +و این کد درباره موقعیت آنها در آرایه مورد نظر جزئیات بیشتری دارد: ```js run ["Bilbo", "Gandalf", "Nazgul"].forEach((item, index, array) => { @@ -225,22 +225,22 @@ And this code is more elaborate about their positions in the target array: }); ``` -The result of the function (if it returns any) is thrown away and ignored. +نتیجه تابع (اگر چیزی برگرداند) نادیده گرفته و دور ریخته می‌شود. -## Searching in array +## جستجو در آرایه -Now let's cover methods that search in an array. +حال بیایید متدهایی را بخوانیم که در آرایه جستجو می‌کنند. -### indexOf/lastIndexOf and includes +### متدهای indexOf/lastIndexOf and includes -The methods [arr.indexOf](mdn:js/Array/indexOf), [arr.lastIndexOf](mdn:js/Array/lastIndexOf) and [arr.includes](mdn:js/Array/includes) have the same syntax and do essentially the same as their string counterparts, but operate on items instead of characters: +متدهای [arr.indexOf](mdn:js/Array/indexOf)، [arr.lastIndexOf](mdn:js/Array/lastIndexOf) و [arr.includes](mdn:js/Array/includes) سینتکس مشابه دارند و اساسا همان کار همتایان خود در رشته‌ها را انجام می‌دهند، اما به جای کاراکترها با المان‌ها کار دارند: -- `arr.indexOf(item, from)` -- looks for `item` starting from index `from`, and returns the index where it was found, otherwise `-1`. -- `arr.lastIndexOf(item, from)` -- same, but looks for from right to left. -- `arr.includes(item, from)` -- looks for `item` starting from index `from`, returns `true` if found. +- `arr.indexOf(item, from)` -- با شروع از ایندکس `from` به دنبال `item` می‌گردد و ایندکسی که المان در آن پیدا شد را برمی‌گرداند، در غیر این صورت `1-`. +- `arr.lastIndexOf(item, from)` -- شبیه متد بالا، اما از راست به چپ جستجو می‌کند. +- `arr.includes(item, from)` -- با شروع از ایندکس `from` به دنبال `item` می‌گردد، اگر پیدا کند `true` را برمی‌گرداند. -For instance: +برای مثال: ```js run let arr = [1, 0, false]; @@ -252,41 +252,41 @@ alert( arr.indexOf(null) ); // -1 alert( arr.includes(1) ); // true ``` -Note that the methods use `===` comparison. So, if we look for `false`, it finds exactly `false` and not the zero. +توجه داشته باشید که متدها از مقایسه `===` استفاده می‌کنند. پس اگر ما به دنبال `false` باشیم، متد دقیقا `false` را پیدا می‌کند و نه صفر را. -If we want to check for inclusion, and don't want to know the exact index, then `arr.includes` is preferred. +اگر ما می‌خواهیم شامل بودن را بررسی کنیم و به دنبال ایندکس دقیق نیستیم، پس `arr.includes` ترجیح داده می‌شود. -Also, a very minor difference of `includes` is that it correctly handles `NaN`, unlike `indexOf/lastIndexOf`: +همچنین، یک تفاوت بسیار کوچک `includes` این است که این متد به درستی `NaN` را کنترل می‌کند، درست برعکس `indexOf/lastIndexOf`: ```js run const arr = [NaN]; -alert( arr.indexOf(NaN) ); // -1 (should be 0, but === equality doesn't work for NaN) -alert( arr.includes(NaN) );// true (correct) +alert( arr.indexOf(NaN) ); // -1 (کار نمی‌کند NaN باید 0 باشد، اما برابری === برای) +alert( arr.includes(NaN) );// true (درست است) ``` -### find and findIndex +### متدهای find and findIndex -Imagine we have an array of objects. How do we find an object with the specific condition? +تصور کنید که یک آرایه‌ای از شیءها داریم. چگونه باید یک شیء با شرطی مشخص را پیدا کنیم؟ -Here the [arr.find(fn)](mdn:js/Array/find) method comes in handy. +اینجاست که متد [arr.find(fn)](mdn:js/Array/find) بدرد می‌خورد. -The syntax is: +سینتکس آن اینگونه است: ```js let result = arr.find(function(item, index, array) { - // if true is returned, item is returned and iteration is stopped - // for falsy scenario returns undefined + // برگردانده شود، المان برگردانده می‌شود و حلقه‌ی تکرار متوقف می‌شود true اگر مقدار + // برگردانده می‌شود undefined مقدار falsy برای سناریوهای }); ``` -The function is called for elements of the array, one after another: +تابع برای المان‌های آرایه، یکی پس از دیگری، صدا زده می‌شود: -- `item` is the element. -- `index` is its index. -- `array` is the array itself. +- `item` المان است. +- `index` ایندکس آن است. +- `array` خود آرایه است. -If it returns `true`, the search is stopped, the `item` is returned. If nothing found, `undefined` is returned. +اگر `true` برگرداند، جستجو متوقف می‎شود، `item` برگردانده می‌شود. اگر چیزی پیدا نشود، `undefined` برگردانده می‌شود. -For example, we have an array of users, each with the fields `id` and `name`. Let's find the one with `id == 1`: +برای مثال، ما یک آرایه‌ای از کاربران داریم، که هر کدام دارای `id` و `name` هستند. بیایید کاربری که `id == 1` داشته باشد را پیدا کنیم: ```js run let users = [ @@ -300,28 +300,28 @@ let user = users.find(item => item.id == 1); alert(user.name); // John ``` -In real life arrays of objects is a common thing, so the `find` method is very useful. +در واقعیت، آرایه‌هایی از شیءها چیز متداولی است، پس متد `find` بسیار مفید است. -Note that in the example we provide to `find` the function `item => item.id == 1` with one argument. That's typical, other arguments of this function are rarely used. +توجه داشته باشید که در مثال بالا ما تابع `item => item.id == 1` را همراه با یک آرگومان برای `find` در نظر گرفتیم. این چیز معمولی است، بقیه آرگومان‌های این تابع به ندرت استفاده می‌شوند. -The [arr.findIndex](mdn:js/Array/findIndex) method is essentially the same, but it returns the index where the element was found instead of the element itself and `-1` is returned when nothing is found. +متد [arr.findIndex](mdn:js/Array/findIndex) اساسا یکسان است، اما به جای خود المان ایندکسی که المان در آن پیدا شد را برمی‌گرداند و اگر چیزی پیدا نشد `1-` را برمی‌گرداند. -### filter +### متد filter -The `find` method looks for a single (first) element that makes the function return `true`. +متد `find` برای یک (اولین) المان که باعث شود تابع `true` برگرداند، جستجو می‌کند. -If there may be many, we can use [arr.filter(fn)](mdn:js/Array/filter). +اگر ممکن باشد تعداد بیشتری موجود باشند، می‌توانیم از [arr.filter(fn)](mdn:js/Array/filter) استفاده کنیم. -The syntax is similar to `find`, but `filter` returns an array of all matching elements: +سینتکس آن مشابه `find` است اما `filter` یک آرایه از المان‌های منطبق را برمی‌گرداند: ```js let results = arr.filter(function(item, index, array) { - // if true item is pushed to results and the iteration continues - // returns empty array if nothing found + // باشد المان به نتیجه‌ها اضافه می‌شود و حلقه تکرار ادامه پیدا می‌کند true اگر + // اگر چیزی پیدا نشود یک آرایه خالی برمی‌گرداند }); ``` -For instance: +برای مثال: ```js run let users = [ @@ -330,74 +330,74 @@ let users = [ {id: 3, name: "Mary"} ]; -// returns array of the first two users +// آرایه شامل دو کاربر اول را برمی‌گرداند let someUsers = users.filter(item => item.id < 3); alert(someUsers.length); // 2 ``` -## Transform an array +## تغییر شکل دادن آرایه -Let's move on to methods that transform and reorder an array. +بیایید به سراغ متدهایی برویم که یک آرایه را تغییر شکل و نظم دوباره می‌دهند. -### map +### متد map -The [arr.map](mdn:js/Array/map) method is one of the most useful and often used. +متد [arr.map](mdn:js/Array/map) یکی از پرکاربردترین و متدوال‌ترین متدهاست. -It calls the function for each element of the array and returns the array of results. +این متد یک تابع را برای هر المان آرایه صدا می‌زند و آرایه‌ای از نتیجه را برمی‌گرداند. -The syntax is: +سینتکس آن اینگونه است: ```js let result = arr.map(function(item, index, array) { - // returns the new value instead of item + // به جای المان، مقدار جدید را برمی‌گرداند }); ``` -For instance, here we transform each element into its length: +برای مثال، ما هر المان را به طول آن تغییر می‌دهیم: ```js run let lengths = ["Bilbo", "Gandalf", "Nazgul"].map(item => item.length); alert(lengths); // 5,7,6 ``` -### sort(fn) +### متد sort(fn) -The call to [arr.sort()](mdn:js/Array/sort) sorts the array *in place*, changing its element order. +صدازدن [arr.sort()](mdn:js/Array/sort) آرایه را *در محل* با تغییر دادن ترتیب المان‌ها، مرتب می‌کند. -It also returns the sorted array, but the returned value is usually ignored, as `arr` itself is modified. +همچنین این متد آرایه مرتب شده را برمی‌گرداند، اما همانطور که خود `arr` تغییر داده می‌شود، مقدار برگردانده شده معمولا نادیده گرفته می‌شود. -For instance: +برای مثال: ```js run let arr = [ 1, 2, 15 ]; -// the method reorders the content of arr +// را دوباره ترتیب بندی می‌کند arr این متد arr.sort(); alert( arr ); // *!*1, 15, 2*/!* ``` -Did you notice anything strange in the outcome? +چیز عجیبی را در نتیجه متوجه شدید؟ -The order became `1, 15, 2`. Incorrect. But why? +ترتیب المان‌ها `1, 15, 2` شد. این اشتباه است. اما چرا؟ -**The items are sorted as strings by default.** +**المان‌ها به صورت پیشفرض به عنوان رشته مرتب می‌شوند.** -Literally, all elements are converted to strings for comparisons. For strings, lexicographic ordering is applied and indeed `"2" > "15"`. +به طور کلی، تمام المان‌ها برای انجام مقایسه به رشته تبدیل می‌شوند. برای رشته‌ها، ترتیب‌بندی لفت‌نامه‌ای اعمال می‌شود و در این صورت `"2" > "15"` است. -To use our own sorting order, we need to supply a function as the argument of `arr.sort()`. +برای استفاده از ترتیب‌بندی خودمان، ما نیاز داریم که یک تابع را به عنوان آرگومان `arr.sort()` قرار دهیم. -The function should compare two arbitrary values and return: +تابع باید دو مقدار دلخواه را مقایسه کند و چیزی را برگرداند: ```js function compare(a, b) { - if (a > b) return 1; // if the first value is greater than the second - if (a == b) return 0; // if values are equal - if (a < b) return -1; // if the first value is less than the second + if (a > b) return 1; // اگر مقدار اول بزرگتر از دومی باشد + if (a == b) return 0; // اگر مقدارها برابر باشند + if (a < b) return -1; // اگر مقدار اول کمتر از دومی باشد } ``` -For instance, to sort as numbers: +برای مثال، برای مرتب کردن به عنوان اعداد: ```js run function compareNumeric(a, b) { @@ -415,13 +415,13 @@ arr.sort(compareNumeric); alert(arr); // *!*1, 2, 15*/!* ``` -Now it works as intended. +حالا همانطور که انتظار می‌رفت کار می‌کند. -Let's step aside and think what's happening. The `arr` can be array of anything, right? It may contain numbers or strings or objects or whatever. We have a set of *some items*. To sort it, we need an *ordering function* that knows how to compare its elements. The default is a string order. +بیایید کمی عقب بمانیم و ببینیم چه چیزی در حال اتفاق افتادن است. `arr` می‌تواند آرایه‌ای از هر چیزی باشد نه؟ ممکن است شامل اعداد یا رشته‌ها یا شیءها یا هرچیز دیگری باشد. ما دسته‌ای از *چیزها* داریم. برای مرتب کردن آن، ما به یک *تابع مرتب‌کننده* که می‌داند چگونه المان‌های دسته را مقایسه کند، نیاز داریم. ترتیب رشته‌ای پیش‌فرض است. -The `arr.sort(fn)` method implements a generic sorting algorithm. We don't need to care how it internally works (an optimized [quicksort](https://en.wikipedia.org/wiki/Quicksort) or [Timsort](https://en.wikipedia.org/wiki/Timsort) most of the time). It will walk the array, compare its elements using the provided function and reorder them, all we need is to provide the `fn` which does the comparison. +متد `arr.sort(fn)` یک الگوریتم مرتب‌سازی کلی را پیاده‌سازی می‌کند. ما نیازی نداریم که بدانیم درون آن چه اتفاقی می‌افتد (اکثر اوقات از یک [مرتب‌سازی سریع](https://fa.wikipedia.org/wiki/مرتب%E2%80%8Cسازی_سریع) یا [Timsort](https://fa.wikipedia.org/wiki/مرتب%E2%80%8Cسازی_تیم) بهینه‌شده استفاده می‌شود). این متد آرایه را طی می‌کند، المان‌های آن را با استفاده از تابع فراهم شده مقایسه می‌کند و آنها را مرتب می‌کند، تمام آن چیزی که ما نیاز داریم این است که یک `fn` فراهم کنیم که مقایسه را انجام دهد. -By the way, if we ever want to know which elements are compared -- nothing prevents from alerting them: +راستی، اگر ما بخواهیم بدانیم که کدام المان‌ها مقایسه می‌شوند -- چیزی ما را از alert کردن آنها متوقف نمی‌کند: ```js run [1, -2, 15, 2, 0, 8].sort(function(a, b) { @@ -430,12 +430,12 @@ By the way, if we ever want to know which elements are compared -- nothing preve }); ``` -The algorithm may compare an element with multiple others in the process, but it tries to make as few comparisons as possible. +الگوریتم ممکن است یک المان را با چند المان دیگر در حین فرایند مقایسه کند، اما تلاش می‌کند که تا جایی که می‌تواند مقایسه‌های کمی انجام دهد. -````smart header="A comparison function may return any number" -Actually, a comparison function is only required to return a positive number to say "greater" and a negative number to say "less". +````smart header="یک تابع مقایسه می‌تواند هر عددی برگرداند" +در واقع یک تابع مقایسه فقط نیاز دارد که یک عدد مثبت را برای اینکه بگوید «بزرگتر» است برگرداند و یک عدد منفی را برای گفتن «کمتر» است. -That allows to write shorter functions: +این ویژگی سبب می‌شود که تابع‌های کوتاه‌تری نوشته شود: ```js run let arr = [ 1, 2, 15 ]; @@ -446,37 +446,37 @@ alert(arr); // *!*1, 2, 15*/!* ``` ```` -````smart header="Arrow functions for the best" -Remember [arrow functions](info:arrow-functions-basics)? We can use them here for neater sorting: +````smart header="توابع پیکانی بهترین‌اند" +[تابع‌های پیکانی](info:arrow-functions-basics) را به یاد دارید؟ ما می‌توانیم از آنها برای مرتب‌سازی تمیزتر استفاده کنیم: ```js arr.sort( (a, b) => a - b ); ``` -This works exactly the same as the longer version above. +این کد دقیقا مانند نسخه طولانی‌تر بالایی کار می‌کند. ```` -````smart header="Use `localeCompare` for strings" -Remember [strings](info:string#correct-comparisons) comparison algorithm? It compares letters by their codes by default. +````smart header="برای رشته‌ها از `localeCompare` استفاده کنید" +الگوریتم مقایسه [رشته‌ها](info:string#correct-comparisons) را به یاد دارید؟ این الگوریتم به صورت پیش‌فرض حروف را با کدهای آنها مقایسه می‌کند. -For many alphabets, it's better to use `str.localeCompare` method to correctly sort letters, such as `Ö`. +برای بساری از حروف الفبا، بهتر است از متد `str.localeCompare` برای مرتب‌کردن صحیح حروف استفاده شود، مانند `Ö`. -For example, let's sort a few countries in German: +برای مثال، بیایید چند کشور را به زبان آلمانی مرتب کنیم: ```js run let countries = ['Österreich', 'Andorra', 'Vietnam']; -alert( countries.sort( (a, b) => a > b ? 1 : -1) ); // Andorra, Vietnam, Österreich (wrong) +alert( countries.sort( (a, b) => a > b ? 1 : -1) ); // Andorra, Vietnam, Österreich (اشتباه است) -alert( countries.sort( (a, b) => a.localeCompare(b) ) ); // Andorra,Österreich,Vietnam (correct!) +alert( countries.sort( (a, b) => a.localeCompare(b) ) ); // Andorra,Österreich,Vietnam (درست است!) ``` ```` -### reverse +### متد reverse -The method [arr.reverse](mdn:js/Array/reverse) reverses the order of elements in `arr`. +متد [arr.reverse](mdn:js/Array/reverse) ترتیب المان‌ها را `arr` برعکس می‌کند. -For instance: +برای مثال: ```js run let arr = [1, 2, 3, 4, 5]; @@ -485,15 +485,15 @@ arr.reverse(); alert( arr ); // 5,4,3,2,1 ``` -It also returns the array `arr` after the reversal. +همچنین این متد ارایه `arr` را بعد از برعکس شدن برمی‌گرداند. -### split and join +### متدهای split and join -Here's the situation from real life. We are writing a messaging app, and the person enters the comma-delimited list of receivers: `John, Pete, Mary`. But for us an array of names would be much more comfortable than a single string. How to get it? +یک موقعیت در زندگی واقعی را می‌گوییم. ما در حال نوشتن یک برنامه پیام‌رسان هستیم و شخص لیستی از دریافت کنندگان که با کاما جدا شده‌اند را وارد می‌کند: `John, Pete, Mary`. اما یک آرایه‌ای از اسم‌ها بسیار راحت‌تر از یک رشته خواهد بود. چگونه آن را دریافت کنیم؟ -The [str.split(delim)](mdn:js/String/split) method does exactly that. It splits the string into an array by the given delimiter `delim`. +متد [str.split(delim)](mdn:js/String/split) دقیقا همین کار را انجام می‌دهد. این متد رشته را با استفاده از جداکننده‌ی داده شده `delim` به یک آرایه تقسیم می‌کند. -In the example below, we split by a comma followed by space: +در مثال بالا، ما توسط یک کاما که بعد آن space می‌آید رشته را جدا می‌کنیم: ```js run let names = 'Bilbo, Gandalf, Nazgul'; @@ -501,11 +501,11 @@ let names = 'Bilbo, Gandalf, Nazgul'; let arr = names.split(', '); for (let name of arr) { - alert( `A message to ${name}.` ); // A message to Bilbo (and other names) + alert( `A message to ${name}.` ); // A message to Bilbo (و بقیه اسم‌ها) } ``` -The `split` method has an optional second numeric argument -- a limit on the array length. If it is provided, then the extra elements are ignored. In practice it is rarely used though: +متد `split` یک آرگومان اختیاری دوم هم دارد -- یک محدودیت برای طول آرایه. اگر این آرگومان اضافه شود، سپس المان‌های دیگر نادیده گرفته می‌شوند. گرچه در عمل به ندرت استفاده می‌شود: ```js run let arr = 'Bilbo, Gandalf, Nazgul, Saruman'.split(', ', 2); @@ -513,8 +513,8 @@ let arr = 'Bilbo, Gandalf, Nazgul, Saruman'.split(', ', 2); alert(arr); // Bilbo, Gandalf ``` -````smart header="Split into letters" -The call to `split(s)` with an empty `s` would split the string into an array of letters: +````smart header="جداکردن به حروف" +صدا زدن `split(s)` با یک `s` خالی رشته را به آرایه‌ای از حروف جدا می‌کند: ```js run let str = "test"; @@ -523,27 +523,27 @@ alert( str.split('') ); // t,e,s,t ``` ```` -The call [arr.join(glue)](mdn:js/Array/join) does the reverse to `split`. It creates a string of `arr` items joined by `glue` between them. +صدا زدن [arr.join(glue)](mdn:js/Array/join) عمل برعکس `split` را انجام می‌هد. این متد یک رشته از `arr` می‌سازد که توسط `glue` المان‌ها متصل شده‌اند. -For instance: +برای مثال: ```js run let arr = ['Bilbo', 'Gandalf', 'Nazgul']; -let str = arr.join(';'); // glue the array into a string using ; +let str = arr.join(';'); // آرایه را با استفاده از ; به یک رشته تبدیل کنید alert( str ); // Bilbo;Gandalf;Nazgul ``` -### reduce/reduceRight +### متد reduce/reduceRight -When we need to iterate over an array -- we can use `forEach`, `for` or `for..of`. +زمانی که ما نیاز داشته باشیم که در یک آرایه حلقه بزنیم، می‌توانیم از `forEach`، `for` یا `for..of` استفاده کنیم. -When we need to iterate and return the data for each element -- we can use `map`. +زمانی که ما نیاز داشته باشیم در المان‌ها حلقه بزنیم و داده را برای هر المان برگردانیم، می‌توانیم از `map` استفاده کنیم. -The methods [arr.reduce](mdn:js/Array/reduce) and [arr.reduceRight](mdn:js/Array/reduceRight) also belong to that breed, but are a little bit more intricate. They are used to calculate a single value based on the array. +متدهای [arr.reduce](mdn:js/Array/reduce) و [arr.reduceRight](mdn:js/Array/reduceRight) همچنین به این دسته تعلق دارند، اما کمی پیچیده‌تر هستند. آنها برای محاسبه یک مقدار بر اساس آرایه، استفاده می‌شوند. -The syntax is: +سینتکس اینگونه است: ```js let value = arr.reduce(function(accumulator, item, index, array) { @@ -551,24 +551,24 @@ let value = arr.reduce(function(accumulator, item, index, array) { }, [initial]); ``` -The function is applied to all array elements one after another and "carries on" its result to the next call. +تابع روی تمام المان‌های آرایه اعمال می‌شود و نتیجه خود را به فراخوانی بعدی «منتقل می‌کند». -Arguments: +آرگومان‌ها: -- `accumulator` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided). -- `item` -- is the current array item. -- `index` -- is its position. -- `array` -- is the array. +- `accumulator` -- نتیجه قبلی فراخوانی تابع است، دفعه اول با `initial` برابر است (اگر `initial` وجود داشته باشد). +- `item` -- المان کنونی آرایه است. +- `index` -- موقعیت آن است. +- `array` -- آرایه است. -As function is applied, the result of the previous function call is passed to the next one as the first argument. +همانطور که تابع اعمال می‌شود، نتیجه فراخوانی قبلی به عنوان آرگومان اول به فراخوانی بعدی منتقل می‌شود. -So, the first argument is essentially the accumulator that stores the combined result of all previous executions. And at the end it becomes the result of `reduce`. +بنابراین، اولین آرگومان اساسا همان حافظه‌ای است که نتیجه ترکیب شده تمام فراخوانی‌های قبلی را ذخیره کرده است. و در پایان تبدیل به نتیجه `reduce` می‌شود. -Sounds complicated? +بنظر پیچیده می‌آید؟ -The easiest way to grasp that is by example. +راحت‌ترین راه برای فهمیدن این قضیه، توسط مثال است. -Here we get a sum of an array in one line: +اینجا ما حاصل جمع یک آرایه را در یک خط می‌گیریم: ```js run let arr = [1, 2, 3, 4, 5]; @@ -578,74 +578,74 @@ let result = arr.reduce((sum, current) => sum + current, 0); alert(result); // 15 ``` -The function passed to `reduce` uses only 2 arguments, that's typically enough. +تابعی که به `reduce` داده شد تنها از 2 آرگومان استفاده می‌کند که معمولا کافی است. -Let's see the details of what's going on. +بیایید جزئیات چیزی که در حال انجام است را ببینیم. -1. On the first run, `sum` is the `initial` value (the last argument of `reduce`), equals `0`, and `current` is the first array element, equals `1`. So the function result is `1`. -2. On the second run, `sum = 1`, we add the second array element (`2`) to it and return. -3. On the 3rd run, `sum = 3` and we add one more element to it, and so on... +1. در اجرای اول، `sum` برابر با مقدار `initial` است (آخرین آرگومان `reduce`)، که برابر با `0` است، و `current` اولین المان آرایه است، که برابر با `1` است. پس نتیجه تابع `1` است. +2. در اجرای دوم، `sum = 1`، که ما المان دوم آرایه (`2`) را به آن اضافه و برمی‌گردانیم. +3. در اجرای سوم، `sum = 3` و ما یک المان دیگر به آن اضافه می‌کنیم و... -The calculation flow: +گردش محاسبه: ![](reduce.svg) -Or in the form of a table, where each row represents a function call on the next array element: +یا به شکل یک جدول که هر ردیف نشان‌دهنده یک فراخوانی تابع روی المان بعدی آرایه است: -| |`sum`|`current`|result| +| |`sum`|`current`|نتیجه| |---|-----|---------|---------| -|the first call|`0`|`1`|`1`| -|the second call|`1`|`2`|`3`| -|the third call|`3`|`3`|`6`| -|the fourth call|`6`|`4`|`10`| -|the fifth call|`10`|`5`|`15`| +|فراخوانی اول|`0`|`1`|`1`| +|فراخوانی دوم|`1`|`2`|`3`| +|فراخوانی سوم|`3`|`3`|`6`| +|فراخوانی چهارم|`6`|`4`|`10`| +|فراخوانی پنجم|`10`|`5`|`15`| -Here we can clearly see how the result of the previous call becomes the first argument of the next one. +اینجا ما می‌توانیم به صورت شفاف ببینیم که نتیجه فراخوانی قبلی به اولین آرگومان فراخوانی بعدی تبدیل می‌شود. -We also can omit the initial value: +ما همچنین می‌توانیم مقدار اولیه را حذف کنیم: ```js run let arr = [1, 2, 3, 4, 5]; -// removed initial value from reduce (no 0) +// حذف شد (بدون 0) reduce مقدار اولیه از let result = arr.reduce((sum, current) => sum + current); alert( result ); // 15 ``` -The result is the same. That's because if there's no initial, then `reduce` takes the first element of the array as the initial value and starts the iteration from the 2nd element. +نتیجه یکسان است. به دلیل اینکه اگر مقدار اولیه‌ای وجود نداشته باشد، سپس `reduce` اولین المان آرایه را به عنوان مقدار اولیه انتخاب می‌کند و حلقه‌زدن را از دومین المان شروع می‌کند. -The calculation table is the same as above, minus the first row. +جدول محاسبات مانند بالا است، منتها ردیف اول را ندارد. -But such use requires an extreme care. If the array is empty, then `reduce` call without initial value gives an error. +اما استفاده کردن به این صورت به دقت بسیار بالایی نیاز دارد. اگر آرایه خالی باشد، سپس فراخوانی `reduce` بدون مقدار اولیه ارور می‌دهد. -Here's an example: +یک مثال اینجا داریم: ```js run let arr = []; // Error: Reduce of empty array with no initial value -// if the initial value existed, reduce would return it for the empty arr. +// .آن را برای آرایه خالی برمی‌گرداند reduce ،اگر مقدار اولیه وجود داشت arr.reduce((sum, current) => sum + current); ``` -So it's advised to always specify the initial value. +بنابراین توصیه می‌شود همیشه مقدار اولیه را تعیین کنید. -The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left. +متد [arr.reduceRight](mdn:js/Array/reduceRight) کار یکسان را انجام می‌هد، اما از راست به چپ. -## Array.isArray +##متد Array.isArray -Arrays do not form a separate language type. They are based on objects. +آرایه‌ها شکل جدیدی از انواع داده را شکل نمی‌دهند. آنها بر اساس شیءها هستند. -So `typeof` does not help to distinguish a plain object from an array: +بنابراین `typeof` برای تشخیص یک شیء ساده از آرایه کمکی نمی‌کند: ```js run alert(typeof {}); // object -alert(typeof []); // same +alert(typeof []); // یکسان ``` -...But arrays are used so often that there's a special method for that: [Array.isArray(value)](mdn:js/Array/isArray). It returns `true` if the `value` is an array, and `false` otherwise. +...اما آرایه‌ها به دلیل اینکه اغلب اوقات استفاده می‌شوند، یک متد خاص برای این کار دارند: [Array.isArray(value)](mdn:js/Array/isArray). این متد اگر `value` یک آرایه باشد `true` برمی‌گرداند و در غیر این صورت `false`. ```js run alert(Array.isArray({})); // false @@ -653,25 +653,25 @@ alert(Array.isArray({})); // false alert(Array.isArray([])); // true ``` -## Most methods support "thisArg" +## اکثر متدها از "thisArg" پشتیبانی می‌کنند -Almost all array methods that call functions -- like `find`, `filter`, `map`, with a notable exception of `sort`, accept an optional additional parameter `thisArg`. +تقریبا تمام متدهای آرایه که تابعی را صدا می‌زنند -- مانند `find`، `filter`، `map`، همچنین یک استثنا از `sort`، پارامتر اختیاری اضافی `thisArg` را قبول می‌کنند. -That parameter is not explained in the sections above, because it's rarely used. But for completeness we have to cover it. +این پارامتر به دلیل اینکه به ندرت استفاده می‌شود، در قسمت‌های بالایی گفته نشد. اما برای کامل بودن ما باید آن را پوشش دهیم. -Here's the full syntax of these methods: +سینتکس کامل این متدها در زیر آمده است: ```js arr.find(func, thisArg); arr.filter(func, thisArg); arr.map(func, thisArg); // ... -// thisArg is the optional last argument +// آرگومان اختیاری آخر است thisArg ``` -The value of `thisArg` parameter becomes `this` for `func`. +مقدار `thisArg` برای `func` برابر با `this` خواهد بود. -For example, here we use a method of `army` object as a filter, and `thisArg` passes the context: +برای مثال، اینجا ما از متد شیء `army` به عنوان یک فیلتر استفاده می‌کنیم، و `thisArg` محتوا را رد و بدل می‌کند: ```js run let army = { @@ -690,7 +690,7 @@ let users = [ ]; *!* -// find users, for who army.canJoin returns true +// برمی‌گرداند را پیدا کن true به ازای آنها army.canJoin هایی کهuser let soldiers = users.filter(army.canJoin, army); */!* @@ -699,53 +699,53 @@ alert(soldiers[0].age); // 20 alert(soldiers[1].age); // 23 ``` -If in the example above we used `users.filter(army.canJoin)`, then `army.canJoin` would be called as a standalone function, with `this=undefined`, thus leading to an instant error. +اگر در مثال بالا ما از `users.filter(army.canJoin)` استفاده می‌کردیم، سپس `army.canJoin` به عنوان یک تابع جداگانه صدا زده می‌شد که `this=undefined`، بنابراین درجا به یک ارور برمی‌خوردیم. -A call to `users.filter(army.canJoin, army)` can be replaced with `users.filter(user => army.canJoin(user))`, that does the same. The latter is used more often, as it's a bit easier to understand for most people. +صدازدن `users.filter(army.canJoin, army)` می‌تواند با `users.filter(user => army.canJoin(user))` جایگزین شود، که هردو یکسان هستند. نوع دوم بیشتر استفاده می‌شود، چون برای اکثر مردم مقداری قابل فهم‌تر است. -## Summary +## خلاصه -A cheat sheet of array methods: +برگ تقلبی از متدهای آرایه: -- To add/remove elements: - - `push(...items)` -- adds items to the end, - - `pop()` -- extracts an item from the end, - - `shift()` -- extracts an item from the beginning, - - `unshift(...items)` -- adds items to the beginning. - - `splice(pos, deleteCount, ...items)` -- at index `pos` deletes `deleteCount` elements and inserts `items`. - - `slice(start, end)` -- creates a new array, copies elements from index `start` till `end` (not inclusive) into it. - - `concat(...items)` -- returns a new array: copies all members of the current one and adds `items` to it. If any of `items` is an array, then its elements are taken. +- برای اضافه/حذف کردن المان‌ها: + - `push(...items)` -- المان‌ها را به آخر اضافه می‌کند، + - `pop()` -- یک المان را از آخر حذف می‌کند، + - `shift()` -- یک المان را از آغاز حذف می‌کند، + - `unshift(...items)` -- المان‌هایی را به آغاز اضافه می‌کند. + - `splice(pos, deleteCount, ...items)` -- در ایندکس `pos` به تعداد `deleteCount` المان حذف و `items` را اضافه می‌کند. + - `slice(start, end)` -- با ساختن یک آرایه جدید، المان‌ها را از ایندکس `start` تا `end` (شامل نمی‌شود) در آن کپی می‌کند. + - `concat(...items)` -- یک آرایه جدید را برمی‌گرداند: تمام عضوهای آرایه کنونی را کپی می‌کند و `items` را به آن اضافه می‌کند. اگر هر کدام از `items` آرایه باشد، سپس المان‌های آن اضافه می‌شوند. -- To search among elements: - - `indexOf/lastIndexOf(item, pos)` -- look for `item` starting from position `pos`, return the index or `-1` if not found. - - `includes(value)` -- returns `true` if the array has `value`, otherwise `false`. - - `find/filter(func)` -- filter elements through the function, return first/all values that make it return `true`. - - `findIndex` is like `find`, but returns the index instead of a value. +- برای جستجو در بین المان‌ها: + - `indexOf/lastIndexOf(item, pos)` -- با شروع از موقعیت `pos` به دنبال `item` می‌گردد، ایندکس آن را برمی‌گرداند و در صورتی که پیدا نشود `1-` را برمی‌گرداند. + - `includes(value)` -- اگر آرایه دارای `value` باشد، مقدار `true` را برمی‌گرداند در غیر این صورت `false`. + - `find/filter(func)` -- المان‌ها را از طریق تابع فیلتر می‌کند، اولین/تمام مقدارهایی که سبب می‌شوند تابع `true` برگرداند را برمی‌گرداند. + - `findIndex` مانند `find` است اما به جای مقدار ایندکس را برمی‌گرداند. -- To iterate over elements: - - `forEach(func)` -- calls `func` for every element, does not return anything. +- برای حلقه زدن در یک آرایه: + - `forEach(func)` -- برای تمام المان‌ها تابع `func` را صدا می‌زند، چیزی را برنمی‌گرداند. -- To transform the array: - - `map(func)` -- creates a new array from results of calling `func` for every element. - - `sort(func)` -- sorts the array in-place, then returns it. - - `reverse()` -- reverses the array in-place, then returns it. - - `split/join` -- convert a string to array and back. - - `reduce/reduceRight(func, initial)` -- calculate a single value over the array by calling `func` for each element and passing an intermediate result between the calls. +- برای تغییر شکل یک آرایه: + - `map(func)` -- از نتایج صدازدن `func` برای هر المان، یک آرایه جدید می‌سازد. + - `sort(func)` -- آرایه را در محل مرتب می‌کند، سپس آن را برمی‌گرداند. + - `reverse()` -- آرایه را در محل برعکس می‌کند، سپس آن را برمی‌گرداند. + - `split/join` -- یک رشته را به آرایه تبدیل می‌کند و برعکس. + - `reduce/reduceRight(func, initial)` -- با صدا زدن `func` برای هر المان و رد و بدل کردن یک نتیجه واسطه بین هر فراخوانی، یک مقدار مفرد را در آرایه محاسبه می‌کند. -- Additionally: - - `Array.isArray(arr)` checks `arr` for being an array. +- علاوه بر این: + - `Array.isArray(arr)` بررسی می‌کند که `arr` یک آرایه باشد. -Please note that methods `sort`, `reverse` and `splice` modify the array itself. +لطفا در نظر داشته باشید که متدهای `sort`، `reverse` و `splice` خود آرایه را تغییر می‌دهند. -These methods are the most used ones, they cover 99% of use cases. But there are few others: +متدهای ذکر شده بیشترین استفاده را دارند، آنها 99% موارد استفاده را پوشش می‌دهند. اما چند متد دیگر هم هست: -- [arr.some(fn)](mdn:js/Array/some)/[arr.every(fn)](mdn:js/Array/every) check the array. +- [arr.some(fn)](mdn:js/Array/some)/[arr.every(fn)](mdn:js/Array/every) آرایه را بررسی می‌کنند. - The function `fn` is called on each element of the array similar to `map`. If any/all results are `true`, returns `true`, otherwise `false`. + تابع `fn` رو تمام المان‌های آرایه صدا زده می‌شود درست شبیه `map`. اگر تمام نتایج `true` بود، مقدار `true` را برمی‌گرداند، در غیر این صورت `false`. - These methods behave sort of like `||` and `&&` operators: if `fn` returns a truthy value, `arr.some()` immediately returns `true` and stops iterating over the rest of items; if `fn` returns a falsy value, `arr.every()` immediately returns `false` and stops iterating over the rest of items as well. + این متدها تقریبا شبیه عملگرهای `||` و `&&` رفتار می‌کنند: اگر `fn` مقدار truthy را برگرداند، `arr.some()` درجا `true` را برمی‌گرداند و حلقه زدن روی بقیه المان‌ها را متوقف می‌کند؛ اگر `fn` یک مقدار falsy برگرداند، `arr.every()` فورا `false` را برمی‌گرداند و حلقه زدن در بقیه المان‌ها را متوقف می‌کند. - We can use `every` to compare arrays: + ما می‌توانیم از `every` برای مقایسه آرایه‌ها استفاده کنیم ```js run function arraysEqual(arr1, arr2) { return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]); @@ -754,16 +754,16 @@ These methods are the most used ones, they cover 99% of use cases. But there are alert( arraysEqual([1, 2], [1, 2])); // true ``` -- [arr.fill(value, start, end)](mdn:js/Array/fill) -- fills the array with repeating `value` from index `start` to `end`. +- [arr.fill(value, start, end)](mdn:js/Array/fill) -- آرایه را با مقدار تکرار شونده `value` از ایندکس `start` تا `end` پر می‌کند. -- [arr.copyWithin(target, start, end)](mdn:js/Array/copyWithin) -- copies its elements from position `start` till position `end` into *itself*, at position `target` (overwrites existing). +- [arr.copyWithin(target, start, end)](mdn:js/Array/copyWithin) -- المان‌های خود را از موقعیت `start` تا موقعیت `end` در *خودش* و در موقعیت `target` کپی می‌کند (جایگزین المان موجود می‌شود). -- [arr.flat(depth)](mdn:js/Array/flat)/[arr.flatMap(fn)](mdn:js/Array/flatMap) create a new flat array from a multidimensional array. +- [arr.flat(depth)](mdn:js/Array/flat)/[arr.flatMap(fn)](mdn:js/Array/flatMap) آرایه‌ای یک دست را از آرایه‌ای چند بعدی می‌سازند. -For the full list, see the [manual](mdn:js/Array). +برای دیدن لیست کامل، از [راهنما](mdn:js/Array) استفاده کنید. -From the first sight it may seem that there are so many methods, quite difficult to remember. But actually that's much easier. +با اولین نگاه ممکن است به نظر برسد که متدهای بسیار زیادی وجود دارد و به حافظه سپردن آنها مشکل است. اما در واقع بسیار آسان‌تر است. -Look through the cheat sheet just to be aware of them. Then solve the tasks of this chapter to practice, so that you have experience with array methods. +برای داشتن شناخت از آنها به برگه تقلب نگاه بیاندازید. سپس تکلیف‌های این فصل را برای تمرین انجام دهید تا نسبت به متدهای آرایه تجربه بدست بیاورید. -Afterwards whenever you need to do something with an array, and you don't know how -- come here, look at the cheat sheet and find the right method. Examples will help you to write it correctly. Soon you'll automatically remember the methods, without specific efforts from your side. +پس از آن هر موقع که نیاز داشتید با یک آرایه کاری انجام دهید، و نمی‌دانید چگونه، به این صفحه بیایید، به برگه تقلب نگاهی بیاندازید و متد مناسب را پیدا کنید. مثال‌ها به شما در نوشتن درست آن کمک می‌کنند. به زودی به طور خودکار شما متدها را به حافظه می‌سپارید، بدون تلاش خاصی از جانب خودتان.