Baseline Widely available
reduce()
ë©ìëë ë°°ì´ì ê° ììì ëí´ ì£¼ì´ì§ 리ëì (reducer) í¨ì를 ì¤ííê³ , íëì ê²°ê³¼ê°ì ë°íí©ëë¤.
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue,
);
console.log(sumWithInitial);
// Expected output: 10
리ëì í¨ìë ë¤ ê°ì ì¸ì를 ê°ì§ëë¤.
리ëì í¨ìì ë°í ê°ì ëì°ê¸°ì í ë¹ëê³ , ëì°ê¸°ë ìí ì¤ ì ì§ëë¯ë¡ ê²°êµ ìµì¢ ê²°ê³¼ë íëì ê°ì´ ë©ëë¤.
구문 arr.reduce(callback[, initialValue])
매ê°ë³ì
callback
ë°°ì´ì ê° ììì ëí´ ì¤íí í¨ì. ë¤ì ë¤ ê°ì§ ì¸ì를 ë°ìµëë¤.
accumulator
ëì°ê¸°ë ì½ë°±ì ë°íê°ì ëì í©ëë¤. ì½ë°±ì ì´ì ë°íê° ëë, ì½ë°±ì 첫 ë²ì§¸ í¸ì¶ì´ë©´ì initialValue
를 ì ê³µí ê²½ì°ìë initialValue
ì ê°ì
ëë¤.
currentValue
ì²ë¦¬í íì¬ ìì.
currentIndex
Optional
ì²ë¦¬í íì¬ ììì ì¸ë±ì¤. initialValue
를 ì ê³µí ê²½ì° 0, ìëë©´ 1ë¶í° ììí©ëë¤.
array
Optional
reduce()
를 í¸ì¶í ë°°ì´.
initialValue
Optional
callback
ì ìµì´ í¸ì¶ìì 첫 ë²ì§¸ ì¸ìì ì ê³µíë ê°. ì´ê¸°ê°ì ì ê³µíì§ ìì¼ë©´ ë°°ì´ì 첫 ë²ì§¸ ìì를 ì¬ì©í©ëë¤. ë¹ ë°°ì´ìì ì´ê¸°ê° ìì´ reduce()
를 í¸ì¶íë©´ ì¤ë¥ê° ë°ìí©ëë¤.
ëì ê³ì°ì ê²°ê³¼ ê°.
ì¤ëªreduce()
ë ë¹ ìì를 ì ì¸íê³ ë°°ì´ ë´ì ì¡´ì¬íë ê° ììì ëí´ callback
í¨ì를 í ë²ì© ì¤ííëë°, ì½ë°± í¨ìë ë¤ìì ë¤ ì¸ì를 ë°ìµëë¤:
accumulator
currentValue
currentIndex
array
ì½ë°±ì ìµì´ í¸ì¶ ë accumulator
ì currentValue
ë ë¤ì ë ê°ì§ ê° ì¤ íë를 ê°ì§ ì ììµëë¤. ë§ì½ reduce()
í¨ì í¸ì¶ìì initialValue
를 ì ê³µí ê²½ì°, accumulator
ë initialValue
ì ê°ê³ currentValue
ë ë°°ì´ì 첫 ë²ì§¸ ê°ê³¼ ê°ìµëë¤. initialValue
를 ì ê³µíì§ ììë¤ë©´, accumulator
ë ë°°ì´ì 첫 ë²ì§¸ ê°ê³¼ ê°ê³ currentValue
ë ë ë²ì§¸ì ê°ìµëë¤.
ì°¸ê³ :
initialValue
를 ì ê³µíì§ ìì¼ë©´,reduce()
ë ì¸ë±ì¤ 1ë¶í° ììí´ ì½ë°± í¨ì를 ì¤ííê³ ì²« ë²ì§¸ ì¸ë±ì¤ë ê±´ë ëëë¤.initialValue
를 ì ê³µíë©´ ì¸ë±ì¤ 0ìì ììí©ëë¤.
ë°°ì´ì´ ë¹ì´ìëë° initialValue
ë ì ê³µíì§ ìì¼ë©´ TypeError
ê° ë°ìí©ëë¤. ë°°ì´ì ììê° (ìì¹ì ê´ê³ìì´) íë ë¿ì´ë©´ì initialValue
를 ì ê³µëì§ ìì ê²½ì°, ëë initialValue
ë 주ì´ì¡ì¼ë ë°°ì´ì´ ë¹ ê²½ì°ì ê·¸ ë¨ë
ê°ì callback
í¸ì¶ ìì´ ë°íí©ëë¤.
ë¤ìì ìì ì²ë¼ initialValue
ì ì ê³µíì§ ìì¼ë©´ ì¶ë ¥ ê°ë¥í íìì´ ì¸ ê°ì§ì´ë¯ë¡, ë³´íµ ì´ê¸°ê°ì 주ë ê²ì´ ë ìì í©ëë¤.
var maxCallback = (acc, cur) => Math.max(acc.x, cur.x);
var maxCallback2 = (max, cur) => Math.max(max, cur);
// initialValue ìì´ reduce()
[{ x: 22 }, { x: 42 }].reduce(maxCallback); // 42
[{ x: 22 }].reduce(maxCallback); // { x: 22 }
[].reduce(maxCallback); // TypeError
// map/reduceë¡ ê°ì - ë¹ìê±°ë ë í° ë°°ì´ììë ëìí¨
[{ x: 22 }, { x: 42 }].map((el) => el.x).reduce(maxCallback2, -Infinity);
reduce()
ìë ë°©ì
ë¤ìì ìì 를 ìê°í´ ë´ ìë¤.
[0, 1, 2, 3, 4].reduce(
function (accumulator, currentValue, currentIndex, array) {
return accumulator + currentValue;
},
);
ì½ë°±ì 4ë² í¸ì¶ë©ëë¤. ê° í¸ì¶ì ì¸ìì ë°íê°ì ë¤ìê³¼ ê°ìµëë¤.
callback
accumulator
currentValue
currentIndex
array
ë°í ê° 1ë²ì§¸ í¸ì¶ 0
1
1
[0, 1, 2, 3, 4]
1
2ë²ì§¸ í¸ì¶ 1
2
2
[0, 1, 2, 3, 4]
3
3ë²ì§¸ í¸ì¶ 3
3
3
[0, 1, 2, 3, 4]
6
4ë²ì§¸ í¸ì¶ 6
4
4
[0, 1, 2, 3, 4]
10
reduce()
ê° ë°ííë ê°ì¼ë¡ë ë§ì§ë§ ì½ë°± í¸ì¶ì ë°íê°(10
)ì ì¬ì©í©ëë¤.
ìì í í¨ì ëì ì íì´í í¨ì를 ì ê³µí ìë ììµëë¤. ìë ì½ëë ìì ì½ëì ê°ì 결과를 ë°íí©ëë¤.
[0, 1, 2, 3, 4].reduce((prev, curr) => prev + curr);
reduce()
ì ë ë²ì§¸ ì¸ìë¡ ì´ê¸°ê°ì ì ê³µíë ê²½ì°, ê²°ê³¼ë ë¤ìê³¼ ê°ìµëë¤:
[0, 1, 2, 3, 4].reduce(function (
accumulator,
currentValue,
currentIndex,
array,
) {
return accumulator + currentValue;
}, 10);
accumulator
currentValue
currentIndex
array
ë°íê° 1ë²ì§¸ í¸ì¶ 10
0
0
[0, 1, 2, 3, 4]
10
2ë²ì§¸ í¸ì¶ 10
1
1
[0, 1, 2, 3, 4]
11
3ë²ì§¸ í¸ì¶ 11
2
2
[0, 1, 2, 3, 4]
13
4ë²ì§¸ í¸ì¶ 13
3
3
[0, 1, 2, 3, 4]
16
5ë²ì§¸ í¸ì¶ 16
4
4
[0, 1, 2, 3, 4]
20
ì´ ë reduce()
ê° ê²°ê³¼ë¡ ë°ííë ê°ì 20
ì
ëë¤.
var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
// sum is 6
íì´í í¨ìë¡ë ìì±í ì ììµëë¤.
var total = [0, 1, 2, 3].reduce(
(accumulator, currentValue) => accumulator + currentValue,
0,
);
ê°ì²´ ë°°ì´ììì ê° í©ì°
ê°ì²´ë¡ ì´ë£¨ì´ì§ ë°°ì´ì ë¤ì´ ìë ê°ì í©ì°í기 ìí´ìë ë°ëì ì´ê¸°ê°ì ì£¼ì´ ê° íëª©ì´ ì¬ë¬ë¶ì í¨ì를 ê±°ì¹ëë¡ í´ì¼ í©ëë¤.
var initialValue = 0;
var sum = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(function (
accumulator,
currentValue,
) {
return accumulator + currentValue.x;
}, initialValue);
console.log(sum); // logs 6
íì´í í¨ì(arrow function)ë¡ë ìì±í ì ììµëë¤:
var initialValue = 0;
var sum = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(
(accumulator, currentValue) => accumulator + currentValue.x,
initialValue,
);
console.log(sum); // logs 6
ì¤ì²© ë°°ì´ í¼ì¹ê¸°
var flattened = [
[0, 1],
[2, 3],
[4, 5],
].reduce(function (accumulator, currentValue) {
return accumulator.concat(currentValue);
}, []);
// í¼ì¹ ê²°ê³¼: [0, 1, 2, 3, 4, 5]
íì´í í¨ìë¡ë ìì±í ì ììµëë¤:
var flattened = [
[0, 1],
[2, 3],
[4, 5],
].reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
ê°ì²´ ë´ì ê° ì¸ì¤í´ì¤ ê°ì ì¸ê¸°
var names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];
var countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++;
} else {
allNames[name] = 1;
}
return allNames;
}, {});
// countedNames is:
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
ìì±ì¼ë¡ ê°ì²´ ë¶ë¥í기
var people = [
{ name: "Alice", age: 21 },
{ name: "Max", age: 20 },
{ name: "Jane", age: 20 },
];
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
var key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
var groupedPeople = groupBy(people, "age");
// groupedPeople is:
// {
// 20: [
// { name: 'Max', age: 20 },
// { name: 'Jane', age: 20 }
// ],
// 21: [{ name: 'Alice', age: 21 }]
// }
íì¥ ì°ì°ìì ì´ê¸°ê°ì ì´ì©íì¬ ê°ì²´ë¡ ì´ë£¨ì´ì§ ë°°ì´ì ë´ê¸´ ë°°ì´ ì°ê²°í기
// friends - an array of objects
// where object field "books" - list of favorite books
var friends = [
{
name: "Anna",
books: ["Bible", "Harry Potter"],
age: 21,
},
{
name: "Bob",
books: ["War and peace", "Romeo and Juliet"],
age: 26,
},
{
name: "Alice",
books: ["The Lord of the Rings", "The Shining"],
age: 18,
},
];
// allbooks - list which will contain all friends' books +
// additional list contained in initialValue
var allbooks = friends.reduce(
function (accumulator, currentValue) {
return [...accumulator, ...currentValue.books];
},
["Alphabet"],
);
// allbooks = [
// 'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
// 'Romeo and Juliet', 'The Lord of the Rings',
// 'The Shining'
// ]
ë°°ì´ì ì¤ë³µ í목 ì ê±°
ì°¸ê³ : ì°¸ê³ : Set
ê³¼ Array.from()
ì ì¬ì©í ì ìë íê²½ì´ë¼ë©´, let orderedArray = Array.from(new Set(myArray));
를 ì¬ì©í´ ì¤ë³µ ìì를 ì ê±°í ìë ììµëë¤.
let arr = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
let result = arr.sort().reduce((accumulator, current) => {
const length = accumulator.length;
if (length === 0 || accumulator[length - 1] !== current) {
accumulator.push(current);
}
return accumulator;
}, []);
console.log(result); //[1,2,3,4,5]
íë¡ë¯¸ì¤ë¥¼ ìì°¨ì ì¼ë¡ ì¤íí기
/**
* Runs promises from array of functions that can return promises
* in chained manner
*
* @param {array} arr - promise arr
* @return {Object} promise object
*/
function runPromiseInSequence(arr, input) {
return arr.reduce(
(promiseChain, currentFunction) => promiseChain.then(currentFunction),
Promise.resolve(input),
);
}
// promise function 1
function p1(a) {
return new Promise((resolve, reject) => {
resolve(a * 5);
});
}
// promise function 2
function p2(a) {
return new Promise((resolve, reject) => {
resolve(a * 2);
});
}
// function 3 - will be wrapped in a resolved promise by .then()
function f3(a) {
return a * 3;
}
// promise function 4
function p4(a) {
return new Promise((resolve, reject) => {
resolve(a * 4);
});
}
const promiseArr = [p1, p2, f3, p4];
runPromiseInSequence(promiseArr, 10).then(console.log); // 1200
í¨ì 구ì±ì ìí íì´í í¨ì
// Building-blocks to use for composition
const double = (x) => x + x;
const triple = (x) => 3 * x;
const quadruple = (x) => 4 * x;
// Function composition enabling pipe functionality
const pipe =
(...functions) =>
(input) =>
functions.reduce((acc, fn) => fn(acc), input);
// Composed functions for multiplication of specific values
const multiply6 = pipe(double, triple);
const multiply9 = pipe(triple, triple);
const multiply16 = pipe(quadruple, quadruple);
const multiply24 = pipe(double, triple, quadruple);
// Usage
multiply6(6); // 36
multiply9(9); // 81
multiply16(16); // 256
multiply24(10); // 240
reduce()
ë¡ map()
ìì±
if (!Array.prototype.mapUsingReduce) {
Array.prototype.mapUsingReduce = function (callback, thisArg) {
return this.reduce(function (mappedArray, currentValue, index, array) {
mappedArray[index] = callback.call(thisArg, currentValue, index, array);
return mappedArray;
}, []);
};
}
[1, 2, , 3].mapUsingReduce(
(currentValue, index, array) => currentValue + index + array.length,
); // [5, 7, , 10]
í´ë¦¬í
// ECMA-262ì ì§í ë¨ê³, 5í(Edition), 15.4.4.21
// 참조: http://es5.github.io/#x15.4.4.21
// https://tc39.github.io/ecma262/#sec-array.prototype.reduce
if (!Array.prototype.reduce) {
Object.defineProperty(Array.prototype, "reduce", {
value: function (callback /*, initialValue*/) {
if (this === null) {
throw new TypeError(
"Array.prototype.reduce " + "called on null or undefined",
);
}
if (typeof callback !== "function") {
throw new TypeError(callback + " is not a function");
}
// 1. Let O be ? ToObject(this value).
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// Steps 3, 4, 5, 6, 7
var k = 0;
var value;
if (arguments.length >= 2) {
value = arguments[1];
} else {
while (k < len && !(k in o)) {
k++;
}
// 3. If len is 0 and initialValue is not present,
// throw a TypeError exception.
if (k >= len) {
throw new TypeError(
"Reduce of empty array " + "with no initial value",
);
}
value = o[k++];
}
// 8. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kPresent be ? HasProperty(O, Pk).
// c. If kPresent is true, then
// i. Let kValue be ? Get(O, Pk).
// ii. Let accumulator be ? Call(
// callbackfn, undefined,
// « accumulator, kValue, k, O »).
if (k in o) {
value = callback(value, o[k], k, o);
}
// d. Increase k by 1.
k++;
}
// 9. Return accumulator.
return value;
},
});
}
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4