본문 바로가기

개발

(132)
Lodash Zip() : Reduce로 구현해보기 const originList = [ [1, 2], [3, 4, 5], [6, 7, 8, 9], ]; const reduceZipResult = originList.reduce((acc, cur, idx) => { for (let i = 0; i < cur.length; i++) { let el = acc[i]; if (el === undefined) { el = []; for (let j = 0; j < i; j++) { el[j] = undefined; } //el = Array(cur.length).fill(undefined); } el[idx] = cur[i]; acc[i] = el; } return acc; }, []); console.log(reduceZipResult);
함수형 프로그래밍 - Reduce를 활용하여 Map, Filter 구현하기 let list = [1, 3, 4, 5, 6, 10]; function filter(list, predicate) { let new_list = []; for (let i = 0; i 5; } let filterResult = filter(list, predicate); console.log(filterResult); function myMap(list, func) { let result = []; for (let i = 0; i < list.length; i++) { result...
[Javascript] 프로그래머스 - 같은 숫자는 싫어(스택/큐) function solution(arr) { let answer = []; for(let i = 0; i < arr.length; i++) { if(arr[i] !== arr[i+1]) { answer.push(arr[i]); } } return answer; } arr = [1, 1, 3, 3, 0, 1, 1] 이면 [1, 3, 0, 1] 을 return 합니다. arr = [4, 4, 4, 3, 3] 이면 [4, 3] 을 return 합니다. 얼핏 보면 중복을 제거하는 문제로 보이지만 첫 번째 케이스 [1, 3, 0, 1] 케이스는 중복을 제거한 결과가 아니다. 따라서 단순 중복 제거로 includes() 함수를 쓰면 안 된다. arr[0]과 arr[1]을 비교해서 같은 숫자이면 다시 돌고, 다른 숫..
lodash :: _.zip()과 _.unzip() 구현하기 _.zip() import _ from "lodash"; const list1 = ['a', 'b'] const list2 = [1, 2]; const list3 = [true, false]; /** * 1. Lodash zip() */ const lodashZip = _.zip(list1, list2, list3); console.log(lodashZip); // [ [ 'a', 1, true ], [ 'b', 2, false ] ] /** * 2. myZip() 구현 */ function myZip(...arr) { let zipResult = []; // [0, 0], [1, 0], [2, 0] -> [0, 1], [1, 1], [2, 1] for(let i = 0; i < arr.length-1;..
lodash :: _.uniq(array) 구현하기 _.uniq(array) 함수는 Lodash에서 배열 내의 중복을 처리하는 array 함수이다. 해당 함수를 ES6 Javascript로 짐작하여 구현해봤다. 1. 첫 번째 방법 -> Object.keys()를 활용하여 key값들만 불러와 중복을 처리하는 방법. import _ from "lodash"; const list = [9, 4, 3, 4, 5, 7, 7, 5, 9, 10]; const lodashUniq = _.uniq(list); // [ 9, 4, 3, 5, 7, 10 ] console.log(lodashUniq); /************* * 첫 번째 방법. Object.keys() 방법 * 자동 정렬이 된다. => [ 3, 4, 5, 7, 9, 10 ] **/ function myUn..
Javascript ES6 - Set 기능 내부 구현해보기 const list = [3, 4, 3, 5, 10, 7, 1, 5, 4, 10]; function mySet(list) { let result = {}; for (let i = 0; i < list.length; i++) { if (result[list[i]] === undefined) { result[list[i]] = [list[i]]; } else { result[list[i]].push(list[i]); } } console.log(Object.keys(result)); return result; } console.log(new Set(list)); console.log(mySet(list)); Set 함수를 사용하지 않고 날 것으로 mySet 함수를 구현해보았다. Object 객체를 만들고, ..
Javascript ES6 - map, filter, reduce 함수 내부 동작 구현하기 1. Map function myMap(list, func) { let result = []; for (let i = 0; i (x % 2 === 0 ? true : false)); console.log(example); console.log(myMap(list, (x) => (x % 2 === 0 ? true : false))); map 함수 내부를 구현해보았다. 어려운 것은 아니고, 따로 myMap을 만들어서 내부적으로 어떻게 동작하는지 짐작하여 구현해보았다. 여기에서 중요한..
[Javascript] N개 배열로 순열 조합 문제 Q. N개의 숫자로 이루어진 배열를 받았을 때 조합할 수 있는 순열 경우의 수를 모두 구하자. function doTree(input) { let doTree_result = []; let result = []; if (input.length > 3) { for (let i = 0; i < input.length; i++) { let prev = 1; let copyArr = [...input]; prev = copyArr.splice(i, 1); doTree_result = doTree(copyArr); for (let j = 0; j < doTree_result.length; j++) { doTree_result[j].splice(0, 0, prev); console.log(`doTree_result..