[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/filter [Back]  [Original]

Array.prototype.filter() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.filter()

Baseline
Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20157.

Array filter()

In this article

const words = ["spray", "elite", "exuberant", "destruction", "present"];

const result = words.filter((word) => word.length > 6);

console.log(result);
// Array ["exuberant", "destruction", "present"]

js
filter(callbackFn)
filter(callbackFn, thisArg)

callbackFn

element

index

array

filter()

thisArg

callbackFn this

filter() callbackFn callbackFn callbackFn

callbackFn

filter() this length

filter() 10

js
function isBigEnough(value) {
  return value >= 10;
}

const filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
//  [12, 130, 44]

js
const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

function isPrime(num) {
  for (let i = 2; num > i; i++) {
    if (num % i === 0) {
      return false;
    }
  }
  return num > 1;
}

console.log(array.filter(isPrime)); // [2, 3, 5, 7, 11, 13]

JSON

filter() JSON id

js
const arr = [
  { id: 15 },
  { id: -1 },
  { id: 0 },
  { id: 3 },
  { id: 12.2 },
  {},
  { id: null },
  { id: NaN },
  { id: "undefined" },
];

let invalidEntries = 0;

function filterByID(item) {
  if (Number.isFinite(item.id) && item.id !== 0) {
    return true;
  }
  invalidEntries++;
  return false;
}

const arrByID = arr.filter(filterByID);

console.log("\n", arrByID);
// 
// [{ id: 15 }, { id: -1 }, { id: 3 }, { id: 12.2 }]

console.log(" =", invalidEntries);
//  = 5

filter()

js
const fruits = ["apple", "banana", "grapes", "mango", "orange"];

/**
 * 
 */
function filterItems(arr, query) {
  return arr.filter((el) => el.toLowerCase().includes(query.toLowerCase()));
}

console.log(filterItems(fruits, "ap")); // ['apple', 'grapes']
console.log(filterItems(fruits, "an")); // ['banana', 'mango', 'orange']

callbackFn

array map() ID filter() ID

js
const names = ["JC63", "Bob132", "Ursula89", "Ben96"];
const greatIDs = names
  .map((name) => parseInt(name.match(/\d+/)[0], 10))
  .filter((id, idx, arr) => {
    //  arr 
    if (idx > 0 && id <= arr[idx - 1]) return false;
    if (idx < arr.length - 1 && id <= arr[idx + 1]) return false;
    return true;
  });
console.log(greatIDs); // [132, 96]

array

filter()

filter()

js
console.log([1, , undefined].filter((x) => x === undefined)); // [undefined]
console.log([1, , undefined].filter((x) => x !== 2)); // [1, undefined]

filter()

filter() this length length

js
const arrayLike = {
  length: 3,
  0: "a",
  1: "b",
  2: "c",
  3: "a", // filter()  length  3
};
console.log(Array.prototype.filter.call(arrayLike, (x) => x <= "b"));
// [ 'a', 'b' ]

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.filter


Web Proxy Viewer  |  New URL  |  Original Page