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);
[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 객체를 만들고, ..