GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing fa.javascript.info — جداسازی از طریق تابع

  • آموزش
  • زبان جاوااسکریپت
  • استفاده پیشرفته از تابع‌ها
  • محدوده متغیر، کلوژِر
بازگشت به درس

جداسازی از طریق تابع

اهمیت: 5

ما یک متد درون‌ساخت arr.filter(f) برای آرایه‌ها داریم. این متد تمام المان‌ها را از طریق f جداسازی می‌کند. اگر true برگرداند، سپس آن المان در آرایه حاصل برگردانده می‌شود.

یک مجموعه از جداسازی‌های «آماده استفاده» بسازید:

  • inBetween(a, b) – بین a و b یا برابر با آنها (شامل آنها هم می‌شود).
  • inArray([...]) – در آرایه داده شده.

طریقه استفاده از آنها باید اینگونه باشد:

  • arr.filter(inBetween(3,6)) – تنها مقدارهای بین 3 و 6 را انتخاب کند.
  • arr.filter(inArray([1,2,3])) – تنها المان‌هایی که با یکی از اعداد [1,2,3] برابر هستند را برگرداند.

برای مثال:

/* .. inArray و inBetween کد شما برای */
let arr = [1, 2, 3, 4, 5, 6, 7];

alert( arr.filter(inBetween(3, 6)) ); // 3,4,5,6

alert( arr.filter(inArray([1, 2, 10])) ); // 1,2

باز کردن یک sandbox همراه با تست‌ها.

Filter inBetween

function inBetween(a, b) {
  return function(x) {
    return x >= a && x <= b;
  };
}

let arr = [1, 2, 3, 4, 5, 6, 7];
alert( arr.filter(inBetween(3, 6)) ); // 3,4,5,6

Filter inArray

function inArray(arr) {
  return function(x) {
    return arr.includes(x);
  };
}

let arr = [1, 2, 3, 4, 5, 6, 7];
alert( arr.filter(inArray([1, 2, 10])) ); // 1,2
function inArray(arr) {
  return x => arr.includes(x);
}

function inBetween(a, b) {
  return x => (x >= a && x <= b);
}

باز کردن راه‌حل همراه با تست‌ها درون یک sandbox.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.