你能爲我描述一下嗎?何時使用reduce和reduceRight?
var arr, total;
arr = [1, 2, 3, 4, 5];
total = arr.reduce(function(previous, current) {
return previous + current;
});
// total is 15
你能爲我描述一下嗎?何時使用reduce和reduceRight?
var arr, total;
arr = [1, 2, 3, 4, 5];
total = arr.reduce(function(previous, current) {
return previous + current;
});
// total is 15
在某些情況下,reduce
和reduceRight
之間的區別呢事情:
var arr = ['1', 2, '3', 4, 5];
var totalLeft = arr.reduce(function(previous, current) {
return previous + current;
});
var totalRight = arr.reduceRight(function(previous, current) {
return previous + current;
});
console.log(totalLeft); // 12345
console.log(totalRight); // 9321
但是,由於增加了兩個整數是在JavaScript commutative,它不會在你的例子沒關係,只是比如數學中1 + 2
等於2 + 1
。
對不起,但我認爲這是一個不好的例子,因爲它不健壯,它不是一個現實的例子。任何實施totalLeft或totalRight方法的人都會在函數中強制使用Number類型,這會使您的用例無關緊要。 –
@EliseChant:這取決於你的用例。如果你正在做一些副作用,例如使用承諾順序獲取文件列表,那麼順序顯然很重要。如果傳遞的函數沒有副作用,那麼這只是函數是否關聯的問題。 –
爲降低的順序從左至右,並且它由右是到左爲reduceRight,如下面的代碼段示出了:
var arr = ["1", "2", "3", "4", "5"];
total1 = arr.reduce(function(prev, cur) {
return prev + cur;
});
total2 = arr.reduceRight(function(prev, cur) {
return prev + cur;
});
console.log(total1); // => 12345
console.log(total2); // => 54321
極好的例子。 – FurkanO
ReduceRight是從減少方法不同,因爲它在值開始計算從右到左。
參考:http://www.thesstech.com/javascript/array_redcueright_method
減少實施例:
<!doctype html>
<html>
<head>
<script>
var arr = [1,2,3];
document.write(arr.reduce(function(x,y){return x*y;},4));
</script>
</head>
</html>
從左至右開始:1 * 2 = 2 * 3 = 6 * 4 = 24
價:http://www.thesstech.com/tryme?filename=javascript_array_reduce_method
ReduceRight示例
<!doctype html>
<html>
<head>
<script>
var arr = [4,256];
document.write(arr.reduceRight(function(x,y){return x/y;},1024));
</script>
</head>
</html>
從右側開始向左:256分之1024= 4/4 = 1
編號:http://www.thesstech.com/tryme?filename=javascript_array_reduceright_method
僅對代碼皺眉 – styvane
代碼@ user3100115有什麼問題? –
如果您將縮小示例顯示爲4 * 1 = 4 * 2 = 8 * 3 = 24,而不是將最初的值乘以最後一位 – MattGoldwater
據我瞭解,arr.reduceRight(myFunc)
基本上等同於做: arr.splice(0).reverse().reduce(myFunc)
Array.reduceRight()
是偉大的當:
的計數器。
var bands = {
Beatles: [
{name: "John", instruments: "Guitar"},
{name: "Paul", instruments: "Guitar"},
{name: "George", instruments: "Guitar"},
{name: "Ringo", instruments: "Drums"}]
};
function listBandplayers(bandname, instrument) {
var bandmembers = bands[bandname];
var arr = [ "<B>" , 0 , ` of ${bandmembers.length} ${bandname} play ` , instrument , "</B>",
"\n<UL>" , ...bandmembers , "\n</UL>" ];
var countidx = 1;
return arr.reduceRight((html, item, idx, _array) => {
if (typeof item === 'object') {
if (item.instruments.contains(instrument)) _array[countidx]++;
item = `\n\t<LI data-instruments="${item.instruments}">` + item.name + "</LI>";
}
return item + html;
});
}
console.log(listBandplayers('Beatles', 'Drums'));
/*
<B>1 of 4 Beatles play Drums</B>
<UL>
<LI data-instruments="Guitar">John</LI>
<LI data-instruments="Guitar">Paul</LI>
<LI data-instruments="Guitar">George</LI>
<LI data-instruments="Drums">Ringo</LI>
</UL>
*/
console.log(listBandplayers('Beatles', 'Guitar'));
/*
<B>3 of 4 Beatles play Guitar</B>
<UL>
<LI data-instruments="Guitar">John</LI>
<LI data-instruments="Guitar">Paul</LI>
<LI data-instruments="Guitar">George</LI>
<LI data-instruments="Drums">Ringo</LI>
</UL>
*/
唯一的是計算順序。減少它'從左到右',reduceRight是否'從右到左'。請看下面的例子:
<html>
<head></head>
<body>
<script>
var a = [200, 100, 2];
var c = a.reduce(function(previousVal, currentVal){
previousVal = previousVal/currentVal;
return previousVal;
});
var c = a.reduceRight(function(previousVal, currentVal){
previousVal = previousVal/currentVal;
return previousVal;
});
alert(c);
</script>
</body>
</html>
</html>
如何https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?和https://developer.mozilla。org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight – elclanrs