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

Array.prototype.filter() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

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 2015 ..

filter() , , .

In this article

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

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

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

js
//  
filter((element) => { ... } )
filter((element, index) => { ... } )
filter((element, index, array) => { ... } )

// -
filter(callbackFn)
filter(callbackFn, thisArg)

//  -
filter(function callbackFn(element) { ... })
filter(function callbackFn(element, index) { ... })
filter(function callbackFn(element, index, array){ ... })
filter(function callbackFn(element, index, array) { ... }, thisArg)

callbackFn

-, . true, , false, .

element

.

index

.

array

, filter().

thisArg

, this - callbackFn.

, . , .

filter() callback , , , callback , true. callback ; , . , callback, .

callback :

  1. ;
  2. ;
  3. , .

filter() thisArg, callback this. this undefined. , this, callback, this.

filter() , .

, filter(), callback. , filter(), , callback. , ,

: , , , ( ).

filter() , 10, 10 .

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

let filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
//  filtered   [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
let 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;
}

let arrByID = arr.filter(filterByID);

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

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

filter() .

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

/**
 *        (query)
 */
function filterItems(query) {
  return fruits.filter(function (el) {
    return el.toLowerCase().indexOf(query.toLowerCase()) > -1;
  });
}

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

ES2015

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

/**
 *        (query)
 */
const filterItems = (arr, query) => {
  return arr.filter(
    (el) => el.toLowerCase().indexOf(query.toLowerCase()) !== -1,
  );
};

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

(, )

filter .

js
//   
let words = ["spray", "limit", "exuberant", "destruction", "elite", "present"];

const modifiedWords = words.filter((word, index, arr) => {
  arr[index + 1] += " extra";
  return word.length < 6;
});

console.log(modifiedWords);
//  ,       6,      ,
//    ['spray']

//   
words = ["spray", "limit", "exuberant", "destruction", "elite", "present"];
const appendedWords = words.filter((word, index, arr) => {
  arr.push("new");
  return word.length < 6;
});

console.log(appendedWords);
//     ,  `words`     ,
//   6 : ['spray', 'limit', 'elite']

//  
words = ["spray", "limit", "exuberant", "destruction", "elite", "present"];
const deleteWords = words.filter((word, index, arr) => {
  arr.pop();
  return word.length < 6;
});

console.log(deleteWords);
// ,  'elite'  ,     `words`  ,
//  filter   : ['spray', 'limit']

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


Web Proxy Viewer  |  New URL  |  Original Page