7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptでfilter

Posted at

JavaScriptで配列の要素をフィルターする方法。例えば値が正のものだけを取り出したいとか。

JavaScript 1.6なら、ずばりfilterメソッドがある。
古いブラウザの場合はjQueryのgrepメソッドという、ちょっと分かりにくい名前のメソッドで同じことが出来る。

var nums = [-1, 2, 0, 3];
// JavaScript 1.6+
var pos1 = nums.filter(function(v) {
  return v > 0;
});
// jQuery
var pos2 = $.grep(nums, function(v) {
  return v > 0;
});
7
6
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?