Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

JavaScriptでfilter

More than 5 years have passed since last update.

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;
});
k4200@github
Software developer for 10+ years interested in Scala & graph DB these days.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away