js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似
1)filter是满足条件的留下,是对原数组的过滤;
2)map则是对原数组的加工,映射成一一映射的新数组
var xx = [1, 2, 5, 7];
function pp(x){return x % 2;}
function px(x){return x % 2;}
var m = xx.map(pp);
console.log("m = " + m);
var f = xx.filter(px);
console.log("f = " + f);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
m = 1,0,1,1
f = 1,5,7
时间: 2024-10-26 03:01:33