Javascript/배열 6

Javascript 배열 내장함수(reduce) 숫자와 문자 다루기

1. 배열의 총 합 구하기 (숫자) 0은 초기 accumulator가 됩니다. accumulator는 누적된 값을 의미합니다. const numbers = [1,2,3,4,5]; const sum = numbers.reduce((accumulator, current)=> accumulator+current , 0); console.log(sum); //출력값 15 소스코드 해석: accumulator은 초기값이 0 으로시작하고 current는 배열의 그 다음값을 의미합니다. 그래서 0+1은 1이되고, accumulator값은 1이 됩니다. 다음 accumulator은 1이되고, current는 2가 되어서 1+2 =3 이됩니다. 다음 accumulator은 3이되고, current는 3이 되어서 3+3 ..

Javascript/배열 2021.02.01

Javascript 배열 내장함수(shift, pop, unshift, push, concat, join)

1. shift shift만 선언해도 왼쪽부터 값이 빠집니다. const numbers = [1,2,3,4,5,6,7,8]; const value = numbers.shift(); numbers.shift(); numbers.shift(); numbers.shift(); console.log(value); console.log(numbers); //출력값 1 [ 5, 6, 7, 8 ] 즉, shift는 앞에서부터 원소값을 하나씩 꺼내는것을 의미합니다. 원소값이 없어질때까지 선언후에 계속 shift를 선언해도 "[ ]" 비어있는 배열을 출력합니다. 에러가 생기지 않습니다. 2. pop shift랑 비슷하지만, shift와 달리 오른쪽부터 값이 빠집니다. const numbers = [1,2,3,4,5,6,..

Javascript/배열 2021.01.31

Javascript 배열 내장함수(filter, splice&slice)

1.filter 특정 조건에 만족하는 값을 찾아서 그 원소들을 가지고 새로운 배열로 출력해줍니다. const items=[{ id:1, text: 'hello', done: true }, { id:2, text: 'bye', done: true },{ id:3, text: 'good', done: true },{ id:3, text: 'nice', done: false }]; const tasksNotDone = items.filter(item=>!item.done); console.log(tasksNotDone); //출력값 [ { id: 3, text: 'nice', done: false } ] item=>!item.done 은 item=>item.done===false와 같은 말입니다. 2.spli..

Javascript/배열 2021.01.31

Javascript 배열 내장함수(forEach, map, indexOf, findIndex, find)

1. forEach문 const superheroes = [ '아이언맨', '캡틴 아메리카', '토르', '닥터 스트레인지' ] function print(hero){ console.log(hero); } superheroes.forEach(print); //출력값 아이언맨 캡틴 아메리카 토르 닥터 스트레인지 모두 내장되어있는 데이터를 전부 출력할 수 있는 편리한 기능입니다. 또한 더 짧게 만들 수도 있습니다. const superheroes = [ '아이언맨', '캡틴 아메리카', '토르', '닥터 스트레인지' ] superheroes.forEach(function(hero){ console.log(hero); }); 더깔끔하게 화살표 함수를 이용할 수 있습니다. const superheroes = [..

Javascript/배열 2021.01.31

Javascript 배열 반복문 (for...of, for...in)

1. for...of 배열안에 있는 값으로 작업을 할 때 사용합니다. const numbers = [10,20,30,40,50]; for(let number of numbers){ console.log(number); } //출력값 10 20 30 40 50 2. for...in 객체에 대한 반복적인 작업을 할때 사용합니다. 우선, 객체출력하는것부터 보여드리겠습니다. const numbers = [10,20,30,40,50]; const doggy = { name: '멍멍이', sound: '멍멍', age: 2 }; console.log(Object.entries(doggy)); console.log(Object.keys(doggy)); console.log(Object.values(doggy)); /..

Javascript/배열 2021.01.31

Javascript 배열의 특징(push, length)

1. 한 배열안에 다른종류의 자료형이 들어가도 됩니다. const array = [1,'blabla', {}, [13]]; console.log(array); //출력값 [ 1, 'blabla', {}, [ 13 ] ] 2. 객체로 이루어진 배열을 만들 수 있습니다. const object = [ {name: '멍멍이'}, {name: '야옹이'} ]; console.log(object); //출력값 [ { name: '멍멍이' }, { name: '야옹이' } ] 3. 새로운항목 추가할때 push를 이용합니다.(내장함수입니다.) const object = [ {name: '멍멍이'}, {name: '야옹이'} ]; object.push({ name:'멍뭉이' }); console.log(object);..

Javascript/배열 2021.01.31