[英]Why do we have functions in sort(), reduce(), map(), filter()?
Can someone explain why these array methods in Javascript have functions as parameters? 有人可以解释为什么Javascript中的这些数组方法将函数作为参数吗? An example would be:
一个例子是:
newArray = oldArray.map(
function(val){
return val + 3;
});
also this, 还有这个
array.sort(function(a, b) {
return b - a;
});
I am having trouble understanding how these functions and their parameters play a roll in the actual Array methods. 我很难理解这些函数及其参数如何在实际的Array方法中发挥作用。
First you need to understand, all JavaScript functions are first class which mean: 首先,您需要了解所有JavaScript函数都是一流的 ,这意味着:
reduce(), sort(), map() and filter()
are method of Array object that accept a callback function as its parameter. reduce(), sort(), map() and filter()
是Array对象的方法,它们接受回调函数作为其参数。
For instance: 例如:
reduce()
var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
});
sort()
: sort()
:
var items = ['réservé', 'premier', 'cliché', 'communiqué', 'café', 'adieu'];
items.sort(function (a, b) {
return a.localeCompare(b);
});
map()
: map()
:
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
filter()
: filter()
:
function isBigEnough(value) {
return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
This callback functions are used to define behaviour. 该回调函数用于定义行为。 Like in sort it will sort array in order descending order.
像排序一样,它将按降序对数组进行排序。 That's the definition how you want to sort array or filter array.
这就是您要对数组或过滤器数组进行排序的定义。 You can play around it to understand it better or MDN has good examples.
您可以试玩一下以更好地理解它,或者MDN提供了很好的示例。
var arr = [12,23,1,56,4,32];
alert(arr.sort());
alert(arr.sort(function(a,b) {
return b - a;
} )); }));
Here in case of first example sort method will sort by comparing first digit. 在第一个示例的情况下,排序方法将通过比较第一个数字进行排序。 In order to achieve correct sorting we need to pass our own logic.
为了实现正确的排序,我们需要传递自己的逻辑。
You can use sort for various different sorting by changing logic in callback function. 通过更改回调函数中的逻辑,可以将排序用于各种不同的排序。
Similar is true for other array methods you mentioned. 您提到的其他数组方法也是如此。
In above example of you want to do ascending order you need to 在上面的示例中,您想要升序,您需要
return a - b; 返回a-b;
It is functional programming style 是函数式编程风格
To (over) simplify concept is, these function will loop through all members of that array for you. 为了简化概念,这些函数将为您遍历该数组的所有成员。 It will just plug that function you pass in at the place it need
它只会在您需要的地方插入您传递的功能
Let make an example with map 让我们以地图为例
If you don't use map, the whole thing you need to write is 如果您不使用地图,那么您需要编写的全部内容就是
var oldArray;
var newArray;
for(var i in oldArray){
if(oldArray[i])
newArray.push(oldArray[i] + 3);
}
You could see that everytimes we do something like this. 您可以看到我们每次都这样做。 The thing that will change is the line in
for
block. 将会改变的是
for
块中的行。 So they just make a function for it 所以他们只是为此做一个功能
function map(oldArray,ConvertToSomething){
var newArray;
for(var i in oldArray)
newArray.push(ConvertToSomething(oldArray[i]));
return newArray;
}
var array;
//// The function you pass in here will became that ConvertToSomething above
var newArray = map(array,function(val){ if(val) return val + 3; })
Easy as that. 那样简单。 Same go for
sort
and filter
. 同样进行
sort
和filter
。 sort
will pick a pair of them for you. sort
会为您挑选一对。 filter
is like map
but it will make an array that exclude thing you don't want by just return false or true filter
就像map
但是它将通过返回false或true组成一个数组,该数组排除不需要的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.