JS Optimize code style
Table of contents
λ°°μ΄μ μννλ©΄μ accumulator
μ value
λ₯Ό λν΄μ sum
μ λ§λ€κ³ , λ§μ§λ§μ λ°°μ΄μ ν¬κΈ°λ‘ λλλ λ‘μ§
const data = [1, 2, 3, 4, 5, 6, 1];const reducer = (accumulator, value, index, array) => {const sumOfAccAndVal = accumulator + value;if (index === array.length - 1) {return (sumOfAccAndVal) / array.length;}return sumOfAccAndVal;};const getMean = data.reduce(reducer, 0);console.log(getMean); // 3.142857142857143
μ΄κΈ°κ°μ 0
μΌλ‘ μ€μ νμ§ μμλ 첫 λ²μ§Έ μΈμμΈ data[0]
κ° accumulator
λ‘ λμ΄κ°λ€.
νμ§λ§ μ΄κΈ°κ°μ μ€μ νλ κ²μ΄ λ μμ νλ€.
const data = [1, 2, 3, 4, 5, 6, 1];const reducer = (accumulator, value, index, array) => {const arrLength = array.length; // μμ μ€μ½ν νμμ μ€μΌ μ μλ€.const sumOfAccAndVal = accumulator + value;if (index === arrLength - 1) {return (sumOfAccAndVal) / arrLength;}return sumOfAccAndVal;};const getMean = data.reduce(reducer, 0);console.log(getMean); // 3.142857142857143
const arrLength = array.length
μ κ°μ΄ λ°°μ΄μ κΈΈμ΄λ₯Ό λ³μλ‘ μ μΈν΄μ μ¬μ¬μ©νλ©΄ μμ μ€μ½νλ‘ μ΄λν΄μ νμνκ³ μ°Έμ‘°λ₯Ό νλ²λ§ μννλ©΄ λκΈ° λλ¬Έμ μ±λ₯μ λμΌ μ μλ€.
νμ§λ§ μμ κ°μ κ²½μ°μμλ μ€νλ € λΉν¨μ¨μ μ΄λ€.
if λ¬Έμ΄ λ°°μ΄μ λ§μ§λ§ μμμ λλ¬ νμλ λμνλ 쑰건μ΄κΈ° λλ¬Έμ μμμ λ§μ§λ§μλ§ λ λ²μ μμ μ€μ½ν νμ, μ°Έμ‘°κ° μΌμ΄λκΈ° λλ¬Έμ΄λ€.
μμ μ€μ½νμμ μ°Έμ‘°ν κ°μ μλ‘μ΄ λ³μμ ν λΉνκ³ λ³μλ₯Ό λ€μ μ°Έμ‘°ν΄μΌ νκΈ° λλ¬Έμ΄λ€.
if (index === array.length - 1)
κ°μ΄ μ¬μ©ν λλ λ³μ μ μΈκ³Ό ν λΉμ΄ κ³Όμ μ΄ μ€μ΄λ λ€.
μμ£Ό λ―ΈμΈν μ°¨μ΄μ§λ§ λ°μ΄ν°μ μμ΄ λ§λ€λ©΄ ν° μ°¨μ΄κ° λ°μν μ μλ€.
κ°λ μ±μ μ κ²½μ°μ§ μκ³ μμ£Ό κ²½λ―Έν μ°¨μ΄μ§λ§ μ±λ₯λ§ μκ°νλ€λ©΄ μ΄λ κ² λ§λ€μ μλ€.
const data = [1, 2, 3, 4, 5, 6, 1];const reducer = (accumulator, value, index, array) => {if (index === array.length - 1) (accumulator + value) / array.length;return accumulator + value;};const getMean = data.reduce(reducer, 0);console.log(getMean); // 3.142857142857143
if λ¬Έμ΄ κ°μ λ°νλ§ νλ€λ©΄ returnμ μλ΅ν μ μλ€.
κ·Όλ° μλ΅νλ©΄ μλ°μ€ν¬λ¦½νΈ μμ§μ΄ λ¬Έλ§₯μ νμ ν λ μλμΌλ‘ μ½μ μ ν΄μ£Όλλ°
κ·Έ κ³Όμ μμ μ±λ₯νλ½μ΄ λ°μν κΉ?
μμ£Όμμ£Ό λ―ΈμΈν μ°¨μ΄μ§λ§ returnμ μλ΅νμλκ° λ λΉ¨λλ€. μμ§...?
μλμΌλ‘ μ½μ νλ κ²μ΄ μλλΌ μλ΅ν΄λ κ°μ λ¬Έλ§₯μΌλ‘ μ΄ν΄νκΈ° λλ¬ΈμΈκ°?
ν ν¬λμ΄μ§μ΄λΌλκ° νλ κ³Όμ μ΄ μ€μ΄μ μΈκ°? ν ..
ννν(flatten)
const data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];const flatArrayReducer = (accumulator, value, index, array) => {return accumulator.concat(value);};const flattenedData = data.reduce(flatArrayReducer, []);// [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
const data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];const flatArrayReducer = (accumulator, value, index, array) => {return [...accumulator, ...value];};const flattenedData = data.reduce(flatArrayReducer, []);console.log(flattenedData);
const data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];const flatArrayReducer = (accumulator, value, index, array) => [...accumulator, ...value];const flattenedData = data.reduce(flatArrayReducer, []);console.log(flattenedData);
.concat()
μ μ¬μ©ν κ² λ³΄λ€λ μ€νλ λ μ°μ°μλ₯Ό μ¬μ©ν κ²μ΄ λ λΉ¨λλ€.
νμ΄ν ν¨μμμ μ€κ΄νΈλ₯Ό μλ΅νμ¬ λ¦¬ν΄ν κ² λ³΄λ€ μ€κ΄νΈλ₯Ό μ¬μ©νκ³ λͺ μμ μΌλ‘ returnμ ν κ²μ΄ μμ£Όμμ£Όμμ£Όμμ£Ό κ·Όμν μ°¨μ΄μ§λ§ λ λΉ¨λλ€.
μ...?
var ν€μλκ° μ€μ½νλ‘ μΈμ νλ λ²μμ μ°κ΄μ΄ μμ κ² κ°λ€.
ν¨μ λ²μ μ€μ½ν κ°μ κ²½μ°λ μ€κ΄νΈλ₯Ό μλ΅νλ€λ©΄ μμ§μ΄ λ¬Έλ§₯μ νμ νκ³ μ€μ½νλ₯Ό λ§λ€μ΄μ€μΌ νμ§λ§
if λ¬Έ κ°μ κ²½μ°λ const, letν€μλκ° μλκ² μλλΌλ©΄ μ€μ½νλ₯Ό μλ΅νκΈ° λλ¬ΈμΌκΉ?