Javascript ES6+ Destructuring assignment
Destructuring assignment 배열의 해체할당 var colors = ['red', 'white', 'orange'] var first = colors[0] var second = colors[1] var third = colors[2] console.log(first, second, third) //결과: red white orange const colors = ['red', 'white', 'orange'] const [first, second, third] = colors console.log(first, second, third) //결과: red white orange 위의 두 코드의 결과는 똑같다. ES6+부터는 변수 이름을 배열화할 수 있게 되었다. const colors = ..
2020. 12. 14.
Javascript ES6+ spread operator (펼치기 연산자)
spread operator (펼치기 연산자) var birds = ['eagle', 'pigeon'] var mammals = ['rabbit', 'cat'] var animals = birds.concat('whale').concat(mammals) console.log(animals) const animals2 = [...birds, 'whale', ...mammals] console.log(animals2) //결과: //["eagle", "pigeon", "whale", "rabbit", "cat"] //["eagle", "pigeon", "whale", "rabbit", "cat"] 위와 같이 .concat이라는 메소드를 쓰지 않고도 더 효율적으로 배열을 만들어 낼 수 있다는 것을 볼 수 있다..
2020. 12. 9.
Javascript ES6+ rest parameter (나머지 매개변수)
rest parameter(나머지 매개변수) 구 버전인 ES5에서 썼던 배열을 만들어 주는 argumernt 대신 "...(매개변수)"을 쓴다면 더 쉽게 배열을 만들 수 있다. ...(매개변수) const f = function (x, y, ...rest) { console.log(rest) } f(1, 2, true, null, undefined, 10) //결과; //[true, null, undefined, 10] 위의 결과를 보면 rest 매개변수를 호출한 결과 ...rest만 해줘도 모든 정의된 값들이 배열로서 결과가 나온다는 것을 알 수 있다. const f = function (...rest) { console.log(rest) } f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ..
2020. 12. 9.
Javascript ES6+ forEach, map, reduce 메소드
forEach, map, reduce 메소드 forEach Array.prototype.forEach(callback[, thisArg]) callback : function (currentValue[, index[, originalArray]]) currentValue : 현재값 index : 현재 인덱스 originalArray : 원본 배열 thisArg : this에 할당할 대상. 생략시 global객체 for문을 돌리는 것이랑 같은 개념이다. const a = [ 1, 2, 3 ] a.forEach(function (v, i, arr) { console.log(v, i, arr, this) }, [ 10, 11, 12 ]) //결과: //1 0 [1, 2, 3] [10, 11, 12] //2 1..
2020. 12. 8.