인프런X디프만

· Frontend/JS
기존 함수의 문제 https://dev-scratch.tistory.com/150 [JavaScript] Map + Filter + Reduce 중첩 사용과 함수형 사고 Map, Filter, Reduce 중첩하여 사용 map, filter, reduce 함수를 커스텀으로 생성하고, 이를 중첩하여 사용해봄으로써 함수형 사고에 대해 배우고자 한다. 각각의 함수는 아래와 같이 작성할 수 있다. const m dev-scratch.tistory.com 앞선 게시물에서 여러 개의 함수를 중첩하여 사용하다보니까 코드는 간결하지만 가독성이 그리 좋지 않았다. 그렇기에 이를 보완할 필요가 있어 보인다. const filter = (f, iter) => { const res = []; for (const a of it..
· Frontend/JS
Map, Filter, Reduce 중첩하여 사용 map, filter, reduce 함수를 커스텀으로 생성하고, 이를 중첩하여 사용해봄으로써 함수형 사고에 대해 배우고자 한다. 각각의 함수는 아래와 같이 작성할 수 있다. const map = (f, iter) => { const res = []; for (const a of iter) { res.push(f(a)) } return res; }; const filter = (f, iter) => { const res = []; for (const a of iter) { if (f(a)) res.push(a); } return res; }; const reduce = (f, acc, iter) => { if (!iter) { iter = acc[Symbo..
· Frontend/JS
앞선 이터러블 프로토콜과 map을 공부하면서 사용자 정의로 이터러블한 객체를 순회하면서 값을 매핑했다. const map = (f, iter) => { const res = []; for (const p of iter) { res.push(f(p)); } return res; }; let m = new Map(); m.set('a', 10); m.set('b', 20); const it = m[Symbol.iterator](); it.next(); it.next(); it.next(); map(([k, a]) => [k, a * 2], m); 이터러블을 따르는 객체는 임의로 만든 map 함수에 잘 작용하는 이터러블 객체이다. filter 함수 const products = [ { name: '허니버터칩'..
· Frontend/JS
Map 메서드 const products = [ { name: '허니버터칩', price: 1500 }, { name: '벌집핏자', price: 1800 }, { name: '치킨팝', price: 800 }, { name: '에낙', price: 500 }, ]; 위와 같은 배열이 있다고 할 때, 이 배열 속 요소들의 객체에서 name만 뽑아낼 수 있는 방법은 간단하게 두 가지가 있다. const names = []; for (const product of products) { names.push(product.name); } 이와 같이 순회를 하면서, 각 객체 안에 접근하여 name들을 새로운 빈 배열에 push하면서 증가시키는 방식이다. 이를 함수형 프로그래밍으로 풀어서 접근하면 아래와 같이 작성..
턴태
'인프런X디프만' 태그의 글 목록 (4 Page)