함수 filter()

(함수 array filter()에서 넘어옴)
  다른 뜻에 대해서는 리눅스 필터 문서를 참조하십시오.
filter
함수 array_filter()

1 JavaScript[ | ]

var arr = ['foo', false, -1, null, ''];
console.log( arr.filter( function(a){ return a; } ) ); // ["foo", -1]
function array_filter(arr) { return arr.filter( function(a){ return a; } ) }
var arr = ['foo', false, -1, null, ''];
console.log( array_filter(arr) );
// ["foo", -1]
arr = [1, 4, 3, 2, 1, 2, 3, 2]
console.log( arr.filter(num => num === 2) )
// [ 2, 2, 2 ]

2 PHP[ | ]

$arr = ['foo', false, -1, null, ''];
print_r( array_filter($arr) );
# Array
# (
#     [0] => foo
#     [2] => -1
# )

3 Python[ | ]

Python 3
numbers = [1, 2, 3, 4, 5, 6]
print( list(filter(lambda x: x%2==0, numbers)) )
# [2, 4, 6]
fruits = ['Apple', 'Banana', 'Orange', 'Mango']
print( list(filter(lambda x: len(x)>5, fruits)) )
# ['Banana', 'Orange']
numbers = [1, 2, 3, 4, 5, 6]
print( [x for x in numbers if x%2==0] )
# [2, 4, 6]
def is_even(i):
    if i%2==0: return True
    return False

numbers = [1, 2, 3, 4, 5, 6]
print( list(filter(is_even, numbers)) )
# [2, 4, 6]
fruits = ['Apple', 'Banana', 'Orange', 'Mango']
print( [x for x in fruits if len(x)>5] )
# ['Banana', 'Orange']

4 R[ | ]

v = c(11, 12, 13, 14, 15, 16)
print( v[v%%2==0] )
## [1] 12 14 16
v = c(1, 4, 3, 2, 1, 2, 3, 2)
print( v[v==2] )
## [1] 2 2 2

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}