Javascript过滤器vs地图问题

gra*_*eds 16 javascript map filter

作为我在对象数组中的最小值/最大值的延续,我想知道滤镜与地图的性能比较.

因此,我将对代码中的值进行测试,以便查看FireBug中的结果.

这是代码:

var _vec = this.vec;
min_x = Math.min.apply(Math, _vec.filter(function(el){ return el["x"]; }));
min_y = Math.min.apply(Math, _vec.map(function(el){ return el["x"]; }));
Run Code Online (Sandbox Code Playgroud)

mapPED版本返回正确的结果.但是filtered版本返回NaN.打破它,逐步完成并最终检查结果,看起来内部函数返回x属性,_vec但返回的实际数组filter是未过滤的_vec.

我相信我的用法filter是正确的 - 其他人可以看到我的问题吗?

这是一个简单的测试:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>S:GTC Map Test</title>
</head>
<body>
<script type="text/javascript">
function vector(x,y,z) { this.x = x; this.y =y; this.z=z; }
var vec = [];
vec.push(new vector(1,1,1));
vec.push(new vector(2,2,2));
vec.push(new vector(2,3,3));
var _vec = vec;
min_x = Math.min.apply(Math, _vec.filter(function(el){ return el["x"]; }));
min_y = Math.min.apply(Math, _vec.map(function(el){ return el["x"]; }));

document.write("<br>filter = " + min_x);
document.write("<br>map = " + min_y);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Guf*_*ffa 49

不,该filter方法不返回未取样的数组.它返回一个数组,其中包含内部函数返回true的项.

由于您没有从内部函数返回布尔值,因此该值将转换为boolean,因此对象引用将转换为true.因此,它返回一个包含原始数组中所有项的新数组.

filter方法与该map方法不同.该map方法用于转换数组的每个项目,而该filter方法用于选择数组的某些项目.比较这些方法之间的性能是没有实际意义的,因为只有其中一个方法可以做你想做的事情.

  • 不,映射和过滤器都返回新数组。 (3认同)
  • 啊.看来我误解了过滤功能是如何工作的.我的印象是它将一个修改过的对象返回给一个数组. (2认同)

Nay*_*gam 16

引自:

JavaScript:
David Flanagan 的权威指南

地图()

map()方法将调用它的数组的每个元素传递给您指定的函数,并返回一个包含该函数返回的值的数组.

例如:

a = [1, 2, 3];

b = a.map(function(x) { return x*x; });  // b is [1, 4, 9]
Run Code Online (Sandbox Code Playgroud)

传递给map()的函数的调用方式与传递给forEach()的函数相同.但是,对于map()方法,您传递的函数应返回一个值.注意map()返回一个新数组:它不会修改它所调用的数组.如果该数组是稀疏的,则返回的数组将以相同的方式稀疏:它将具有相同的长度和相同的缺失元素.

过滤()

该方法返回一个数组,该数组包含调用它的数组元素的子集.传递给它的函数应该是谓词:返回true或false的函数.谓词的调用与forEach()和map()一样.如果返回值为true,或者转换为true的值,则传递给谓词的元素是子集的成员,并添加到将成为返回值的数组中.

例子:

a = [5, 4, 3, 2, 1];

smallvalues = a.filter(function(x) { return x < 3 });   // [2, 1]

everyother = a.filter(function(x,i) { return i%2==0 }); // [5, 3, 1]
Run Code Online (Sandbox Code Playgroud)


小智 6

// MAP creates a new array
// MPA return new Array
var arr = [1, 2, 3, 4, 5, 6, 7];

var newArr = arr.map((el) => {
  return el * 2;
});

console.log(newArr); //2,4,3,8,10,12,14

// filter() return new Array
var newFilter = arr.filter((el) => {
   return el * 2;
});
console.log(newFilter); // 1,2,3,4,5,6,7
Run Code Online (Sandbox Code Playgroud)

现在你可以看到我有return el*2地图和过滤器,它们给出了不同的输出

Filter() filter() 方法创建一个新数组,其中填充了通过该函数实现的测试的所有数组元素。

如果此条件返回 true,则该元素将被推送到输出数组。如果条件返回 false,则该元素不会被推送到输出数组。

var arr = [1, 2, 3, 4, 5, 6, 7];
var newFilter = arr.filter((el) => {
  return el > 3;
});
console.log(newFilter);  //[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

Map() map() 方法用于从现有数组创建新数组,对第一个数组的每个元素应用一个函数 var arr = [1, 2, 3, 4, 5, 6, 7 ];

var newArr = arr.map((el) => {
  return el * 2;
});

console.log(newArr); //2,4,3,8,10,12,14
Run Code Online (Sandbox Code Playgroud)