let list = [1, 3, 4, 5, 6, 10];
function filter(list, predicate) {
let new_list = [];
for (let i = 0; i < list.length; i++) {
if (predicate(list[i])) new_list.push(list[i]);
}
return new_list;
}
function predicate(n){
return n > 5;
}
let filterResult = filter(list, predicate);
console.log(filterResult);
function myMap(list, func) {
let result = [];
for (let i = 0; i < list.length; i++) {
result.push(func(list[i]));
}
return result;
}
function square(n) {
return n * n;
}
let mapResult = myMap(list, square);
console.log(mapResult);
//////////////////////////////////////////////
/*
* Reduce로 Map 구현
*/
function myReduceForMap(list, func, init) {
let result = init; // []
for (let i = 0; i < list.length; i++) {
result.push(func(list[i]));
}
return result;
}
function plus(acc, value) {
return parseInt(acc + value);
}
let reduceResult = myReduceForMap(list, plus, []);
console.log(`reduceMapResult : ${mapResult}`);
//////////////////////////////////////////////////////////////
/*
* Reduce로 Filter 구현
*/
function myReduceForFilter(list, func, init) {
let result = init; // []
for (let i = 0; i < list.length; i++) {
if(func(list[i])) {
result.push(list[i]);
}
}
return result;
}
function ageThreeUp(n) {
return n >= 3;
}
let reduceFilterResult = myReduceForFilter(list, ageThreeUp, []);
console.log(`reduceFilterResult : ${reduceFilterResult}`);
//////////////////////////////////////////////////////////////
/*
* Reduce로 Reduce 구현
* ----> 초깃값 없을 때
*/
function myReduce(list, func, init) {
let result = init;
for (let i = 1; i < list.length; i++) {
result = func(list[i-1], list[i]);
}
return result;
}
function multiply(acc, value) {
console.log(`${acc} * ${value} = ${acc * value}`);
acc = acc * value;
return acc;
}
let ReduceMultiply = myReduce(list, multiply, list[0]);
console.log(ReduceMultiply);
//////////////////////////////////////////////////////////////
// 원조(?) reduce 메서드로 map 만들기
const originalReduceMakeMap = list.reduce((accumulator, currentValue, currentIndex) => , []);
console.log(typeof originalReduceMakeMap);
console.log(originalReduceMakeMap);
let list = [1, 3, 4, 5, 6, 10];
위 list 배열을 가지고 map, filter, reduce, reduce를 이용한 map, reduce를 이용한 filter,
ES6의 reduce를 활용하여 map과 filter를 구현해보려고 한다.
Map
function myMap(list, func) {
let result = [];
for (let i = 0; i < list.length; i++) {
result.push(func(list[i]));
}
return result;
}
function square(n) {
return n * n;
}
let mapResult = myMap(list, square);
console.log(mapResult);
Filter
function filter(list, predicate) {
let new_list = [];
for (let i = 0; i < list.length; i++) {
if (predicate(list[i])) new_list.push(list[i]);
}
return new_list;
}
function predicate(n){
return n > 5;
}
let filterResult = filter(list, predicate);
console.log(filterResult);
Reduce For Map
function myReduceForMap(list, func, init) {
let result = init; // []
for (let i = 0; i < list.length; i++) {
result.push(func(list[i]));
}
return result;
}
function plus(acc, value) {
return acc + value;
}
let reduceResult = myReduceForMap(list, plus, []);
console.log(`reduceMapResult : ${mapResult}`);
Reduce For Filter
/*
* Reduce로 Filter 구현
*/
function myReduceForFilter(list, func, init) {
let result = init; // []
for (let i = 0; i < list.length; i++) {
if(func(list[i])) {
result.push(list[i]);
}
}
return result;
}
function ageThreeUp(n) {
return n >= 3;
}
let reduceFilterResult = myReduceForFilter(list, ageThreeUp, []);
console.log(`reduceFilterResult : ${reduceFilterResult}`);
Reduce
/*
* Reduce로 Reduce 구현
* ----> 초깃값 없을 때
*/
function myReduce(list, func, init) {
let result = init;
for (let i = 1; i < list.length; i++) {
result = func(list[i-1], list[i]);
}
}
function multiply(acc, value) {
console.log(`${acc} * ${value} = ${acc * value}`);
acc = acc * value;
return acc;
}
let ReduceMultiply = myReduce(list, multiply, list[0]);
console.log(ReduceMultiply);
'개발 > ES6' 카테고리의 다른 글
Lodash Zip() : Reduce로 구현해보기 (0) | 2023.10.12 |
---|---|
Javascript ES6 - Set 기능 내부 구현해보기 (0) | 2023.10.08 |
Javascript ES6 - map, filter, reduce 함수 내부 동작 구현하기 (1) | 2023.10.08 |