JS array reduce () method advanced techniques and Detailed

JS array reduce () method advanced techniques and Detailed

The basic concept
I have always felt a knowledge point of speaking too easily mislead you, make life scared
reduce () method reduce online that's too complicated, in fact, master these skills you will be able to greatly enhance the development efficiency

Simple application

Example 1: Overlay

var items = [10, 120, 1000];
var total = items.reduce((sumSoFar, item) => {
	return sumSoFar + item;
}, 0);

console.log(total); // 1130

Of course, you can also receive objects

var items = [10, 120, 1000];
var total = items.reduce((sumSoFar, item) => {
	return sumSoFar.sum + item;
}, {sum: 0});

console.log(total); // {sum:1130}

See Example One should be able to understand that the first parameter is the last call to the callback return value, or the value of the initial offer. The second argument is that through the array, in fact, we then project development which mostly do is to manipulate arrays each json object made
welcome everyone into the front-end technology exchange Q group 426,334,209

Guess you like

Origin blog.csdn.net/qq_35715972/article/details/90228605