본문 바로가기

배열 내장함수4

[JS] 배열 내장함수 concat, join, reduce concat 여러개의 배열을 하나의 배열로 합쳐줍니다. const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const concated = arr1.concat(arr2); console.log(concated); join 배열 안의 값들을 '문자열'형태로 합쳐줌 인자로 받은 값을 기준으로 합쳐줌. const array = [1, 2, 3, 4, 5]; console.log(array.join()); // 1,2,3,4,5 console.log(array.join(' ')); // 1 2 3 4 5 console.log(array.join(', ')); // 1, 2, 3, 4, 5 reduce 주어진 배열에 대한 총합을 구할 때, 기존 코드는 아래와 같이 구현하지만 co.. 2020. 8. 12.
[JS] 배열 내장함수 shift, pop, unshift sihft shift는 첫번째 원소를 배열에서 추출(기존 배열에서는 삭제) const numbers = [10, 20, 30, 40]; const value = numbers.shift(5); console.log(value); console.log(numbers); pop 배열의 맨 마지막 항목을 추출(기존 배열에서 삭제) const numbers = [10, 20, 30, 40]; const value = numbers.pop(); console.log(value); console.log(numbers); unshift shift의 반대로 배열의 맨 앞에 새 원소를 추가함 const numbers = [10, 20, 30, 40]; numbers.unshift(5); console.log(number.. 2020. 8. 12.
[JS] 배열 내장함수 filter, splice, slice filter 특정 조건을 만족하는 값들만 따로 추출하여 새로운 배열을 만듦. todos 배열에서 done 값이 false 인 항목들만 따로 추출해서 새로운 배열을 만들어 보면 아래와 같이 할 수 있다. const todos = [ { id: 1, text: '자바스크립트 입문', done: true }, { id: 2, text: '함수 배우기', done: true }, { id: 3, text: '객체와 배열 배우기', done: true }, { id: 4, text: '배열 내장함수 배우기', done: false } ]; const tasksNotDone = todos.filter(todo => todo.done === false); #위의 코드를 아래와 같이 바꾸어도 동작함# const tas.. 2020. 8. 12.
[JS]배열 내장함수 indexOf, findIndex, find indexOf indexOf는 원하는 항목이 몇번째 '원소'인지 찾아주는 함수입니다. (배열 안에 있는 값이 숫자, 문자열, 불리언 일 때) 예를 들어서 다음과 같은 배열이 있을 때 const superheroes = ['아이언맨', '캡틴 아메리카', '토르', '닥터 스트레인지']; 토르가 몇번째 항목인지 찾으려면 아래와 같이 입력하면 됨 const superheroes = ['아이언맨', '캡틴 아메리카', '토르', '닥터 스트레인지']; const index = superheroes.indexOf('토르'); console.log(index); findIndex (배열안에 있는 값이 객체, 배열 일 때) const todos = [ { id: 1, text: '자바스크립트 입문', done: t.. 2020. 8. 11.