ES6如何获取filter()方法的源代码 [英] ES6 how to get source code for filter( ) method
本文介绍了ES6如何获取filter()方法的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我已经尝试重现方法 filter
了:
I've tried to reproduce the method filter
so far I have got:
function filtre(...[T,x,y]){
let tabPersF = [];
for (let pers of T) {
if (x(...[pers,y])) {
tabPersF.push( pers );
}
}
return tabPersF;
}
怎么样?
推荐答案
所以我试着重现我得到的方法过滤器,我知道不好.
So I've tried to reproduce the method filter I got this and I know it's not good.
此代码存在多个问题
-
array2
未初始化 -
array2
不是array
,什么是puts
-
function
关键字作为参数
array2
not initializedarray2
not anarray
and what isputs
function
keyword as parameter
制作
function filter(array, func) {
let array2 = [];
for ( var counter = 0; counter < array.length; counter++) {
if (func(array[counter], counter, array )) {
array2.push(array[counter]);
}
}
return array2;
}
console.log( filter( [1,2,3], function(s){ return s>1 } ) );
console.log( filter( ["132","322","23433"], function(s){ return s.length>3 } ) );
var arr = [ {name: "asdasd", age: 10}, {name: "asdasd2", age: 20} ];
console.log( filter( arr , function(s){ return s.age>19 } ) );
这篇关于ES6如何获取filter()方法的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文