[JS] - ES6

Imomo·2021년 3월 25일
0

filter

메서드는 주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환합니다.

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);  // 길이가 6이상인 문자열로 구성된 배열로 리턴된다.

String Literal

const val1 = 'my string';
const val2 = 'my string2';
const litVal = `${val1}, ${val2}` 
console.log(litVal);
// my string, my string2

2차원배열 원소 정렬

arrs.sort((a,b) =>{
        return a[2] - b[2];
    });

최댓값, 최소값

answer = Math.max(answer, dy[i]);

반복문 원소 여러개 출력

for (const [t, f, v] of fares)
        d[t - 1][f - 1] = d[f - 1][t - 1] = v;

배열 , 2차원배열 만들기

const visit = Array.from({length: n+1}, () => 0);
0으로 초기화된 배열
const arr = Array.from(Array(n+1), () => Array(n+1).fill(Infinity));
Infinity 무한으로 초기화

2차원 인접리스트 만들기 (DFS, BFS)

let graph = Array.from(Array(n+1), () => []);

조건 변수

const name = ''
const userName = num || 'undefined';
console.log(userName);

const num = 0;
const message = num ?? 'undefined';
console.log(message);

값이 있는지 없는지 파악해서 변수에 넣어줌

|| 의 경우 '' 값을 null값으로 인식
?? 의 경우 '' 값으로 인식

reduce

  • 배열을 순회하며 인덱스 데이터를 줄여가며 어떠한 기능을 수행 할 수 있다.

기능1 (배열 원소들의 합 구하기)

let total = [1, 2, 3, 4, 5].reduce(
  ( acc, curr ) => acc + curr, 
  0
);
console.log(total) // 결과값: 15

let total=arr.reduce((a, b)=>a+b, 0);

기능2 (배열 원소들 중 최대값 구하기)

let max = [1, 2, 3, 4, 5].reduce(
  ( max, cur ) => Math.max( max, cur ), 
  -Infinity
);
console.log(max) // 결과값: 5

0개의 댓글