const animals = [ { name: 'doggo', age: 0 }, { name: 'catch', age: 3 }, { name: 'birdy', age: 1 }, ]; const map = new Map(animals.map(animal => [animal.name, animal.age])); console.log(map); [JS] 배열을 map으로 변환하기 자바스크립트 array 를 object, Map 으로 변환하기 코틀린을 쓰다보면 associate 메소드를 이용하여 리스트를 맵으로 쉽게 바꾸어 사용합니다. js, ts 에서는 코틀린의 associate 처럼 메소드로 제공되어 있 ingnoh.tistory.com 기존에 작성한 위 방식보다 더 직관적인 듯!
JS의 객체를 저장하는 변수는 객체의 주소를 참조한다. Object.assign({}, target) 명령어는 새로운 객체를 만들고 원본 객체의 열거 가능한 속성을 복사한다. spread 연산자 ...도 마찬가지! const origin = { id: 0, name: 'injuk', age: '1', }; // 참조를 복사하므로 두 객체는 같다. const obj1 = origin; check(obj1); // 새로운 빈 객체를 만들고 원본의 속성을 복사하므로 두 객체는 다르다. const obj2 = Object.assign({}, origin); check(obj2); // 새로운 객체를 만들고, 원본 객체에 새로운 객체의 속성을 복사하므로 두 객체는 같다. const obj3 = Object.ass..
[변수] instanceof Function을 활용하여 체크한다. typeof [변수] === 'function'도 사용할 수 있다. 사용 예시 function callFunctions(init, ...functions) { return functions.reduce((acc, func) => { if(func instanceof Function) return func(acc); else throw new Error(`${func} is not Function`); }, init); } try { const result = callFunctions( 1, value => value *= 100, value => value += 2, // 3, value => value -= 22, console.log,..
Correct way to convert size in bytes to KB, MB, GB in JavaScript I got this code to covert size in bytes via PHP. Now I want to convert those sizes to human readable sizes using JavaScript. I tried to convert this code to JavaScript, which looks like this: fun... stackoverflow.com 리마인드용으로 작성
[BaekJoon] 2309 일곱난쟁이 (node.js) 2309번: 일곱 난쟁이 아홉 개의 줄에 걸쳐 난쟁이들의 키가 주어진다. 주어지는 키는 100을 넘지 않는 자연수이며, 아홉 난쟁이의 키는 모두 다르며, 가능한 정답이 여러 가지인 경우에는 아무거나 ingnoh.tistory.com 요 문제를 보다가 기본적인 for문을 통해 배열을 순회하고 있는 것을 보고 의문이 들었다. 기본적인 for문과 forEach, for in문에서 가능한 것처럼 for of문에서도 배열 인덱스를 사용할 수 있지 않을까? 결론부터 말하자면 Array.prototype.keys()나 entries()를 활용하는 방법이 있었다. 이 글은 다음과 같은 사람에게 도움이 될 것으로 보인다. 기본적인 for 문의 문법이 너무 어려워 ..
Promise.allSettled() - JavaScript | MDN Promise.allSettled() 메서드는 주어진 모든 프로미스를 이행하거나 거부한 후, 각 프로미스에 대한 결과를 나타내는 객체 배열을 반환합니다. developer.mozilla.org // 1. const promises = [ // some promises... ]; // 2. const responses = await Promise.allSettled(promises); // 3. let results = []; for(const { status, value = undefined } of responses) { if(status === 'fulfilled') results.push(value); } Promise들을 포함..
const before = [1, 2, 3, 0, false, true, undefined, null, {}]; console.log(`before: ${before} and length is ${before.length}`); function removeFalsy(elements = []) { return elements.filter(elem => elem); } const after = removeFalsy(before); console.log(`after: ${after} and length is ${after.length}`); /* 실행 결과 before: 1,2,3,0,false,true,,,[object Object] and length is 9 after: 1,2,3,true,[object..
- Total
- Today
- Yesterday
- Linux
- RancherDesktop
- postgresql
- 코딩테스트
- Spring Cloud Config
- JPA
- Git
- etc
- shell
- eureka
- AWS IoT
- JEST
- Gradle
- hashicorp
- Database
- AWS
- IntelliJ
- Puppeteer
- mysql
- terraform
- spring boot
- pgloader
- jQuery
- kotlin
- Node.js
- react
- Vault
- Docker
- javascript
- Java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |