JAVASCRIPT - 초급 9편

MJ·2022년 6월 30일
0

JAVASCRIPT 정리

목록 보기
9/22
post-thumbnail

* Array 자료형 : 배열

특징

1. 순서대로 값을 저장 ( 순서 개념 O )
2. 인덱스 가능
3. 정렬 가능...
ex )
var car = [ '소나타', 50000, 'white' ];
car[1] = 60000; // 배열에 [1]번째 자리에 있는 값에 대입
car.sort(); // 50000, 'white', '소나타'
			//( 정렬은 기본적으로 숫자, 영어, 한글로 정렬 된다 
console.log(car[1]); // 60000
					 // 배열에 [1]번째 자리에 있는 값을 반환 

* Array Methods : 배열 메소드

1. .at() : ( arrName.at(정수요소) )

정수 값을 받아, 배열에서 해당하는 인덱스의 요소 반환
ex )
var car = [ '소나타', 50000, 'white' ];
var index = 0;
console.log(`Car name is ${car.at(index)}`) // Car name is 소나타

2. .concat()

배열을 합쳐서 새로운 배열 반환
ex )
var car = [ '소나타', 50000, 'white' ];
var car1 = [ '아반떼', 40000, 'black' ];
var carArr = car.concat(car1);
console.log(carArr); // [ '소나타', 50000, 'white', '아반떼', 40000, 'black' ]

3. .every()

배열 안의 모든 요소가 주어진 판별 함수를 통과할때 Boolean 값을 반환
ex )
function isBigEnough(element, index, arr) {
  return element >= 10;
}
var a = [12, 5, 8, 130, 44].every(isBigEnough);
var b = [12, 5, 8, 130, 44].every(elem => elem >= 5); // 화살표함수
console.log(a); // false
console.log(b); // true

4. .sort()

배열을 정렬, ( 숫자 > 영어 > 한글 )
ex )
var car = [ '소나타', 50000, 'white' ];
car.sort(); // 50000, 'white', '소나타'
			// ( 정렬은 기본적으로 숫자, 영어, 한글로 정렬 된다 )

5. .fill()

배열의 시작 인덱스부터 끝 인덱스의 이전까지 정적인 값 하나로 채움
ex )
const array1 = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4)); // [2]번째 자리부터 [4]째 자리이전 까지 0으로 채우기
// expected output: [1, 2, 0, 0]
console.log(array1.fill(5, 1)); // 1번째 자리부터 끝까지 5로 채우기
// expected output: [1, 5, 5, 5]
console.log(array1.fill(6)); // 6으로 전부 채우기
// expected output: [6, 6, 6, 6]

6. .filter()

주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환
ex )
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
// 배열의 요소를 파라미터로 받아 해당 요소의 길이가 6보다 큰 요소를 새로운 배열로 반환
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

7. .find()

주어진 판별 함수를 만족하는 첫 번째 요소의 값을 반환합니다.
그런 요소가 없다면 undefined를 반환
ex )
const array2 = [5, 12, 8, 130, 44];
const found1 = array2.find(element => element > 10);
console.log(found1); // expected output: 12
const found2 = array2.find(element => element > 140);
console.log(found2); // expected output: undifined

8. .findIndex()

주어진 판별 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스를 반환
만족하는 요소가 없으면 -1을 반환
ex )
const array3 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13; // 파라미터가 13보다 큰 조건의 화살표 함수
console.log(array3.findIndex(isLargeNumber)); // expected output: 3
const isLargeNumber = (element) => element > 140;
console.log(array3.findIndex(isLargeNumber)); // expected output: -1

9. .forEach()

주어진 함수를 배열 요소 각각에 대해 실행
ex )
const array4 = ['a', 'b', 'c'];
array4.forEach(element => console.log(element));

10. .indexOf()

배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환
존재하지 않으면 -1을 반환
ex )
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison')); // expected output: 1
// [2]번째 자리부터 시작하여 'bison'이 있는 값의 인덱스 반환 
console.log(beasts.indexOf('bison', 2)); // expected output: 4
console.log(beasts.indexOf('giraffe')); // expected output: -1

11. .lastIndexOf()

배열에서 주어진 값을 발견할 수 있는 마지막 인덱스를 반환
존재하지 않으면 -1을 반환
ex )
var array = [2, 5, 9, 2];
array.lastIndexOf(2);     // 3
array.lastIndexOf(7);     // -1
array.lastIndexOf(2, 3);  // 3
array.lastIndexOf(2, 2);  // 0
array.lastIndexOf(2, -2); // 0
array.lastIndexOf(2, -1); // 3

12. .join()

배열의 모든 요소를 연결해 하나의 문자열
ex )
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join()); // expected output: "Fire,Air,Water"
console.log(elements.join('')); // expected output: "FireAirWater"
console.log(elements.join('-')); // expected output: "Fire-Air-Water"

13. .map()

배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환
ex )
const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2); // 배열에 각 요소 별로 * 2를 해준 값을 리턴하여 새로운 배열로 반환
console.log(map1);
// expected output: Array [2, 8, 18, 32]

14. .pop()

배열에서 마지막 요소를 제거하고 그 요소를 반환
ex )
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// expected output: "tomato"
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]

15. .push()

배열의 끝에 하나 이상의 요소를 추가하고, 배열의 새로운 길이를 반환
ex )
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

16. .reduce()

배열의 각 요소에 대해 주어진 리듀서(reducer) 함수를 실행하고, 하나의 결과값을 반환
ex )
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue, currentValue) => previousValue + currentValue,
  initialValue
);
console.log(sumWithInitial);
// expected output: 10

* Object 자료형 : 객체

특징

1. 순서 X
2. 인덱스 가능

{ key : value }

예시
var a = { name : "javascript" } ;
// key : name / value : javascript
console.log(a['name']); // result : javascript
console.log(a.name); // result : javascript

profile
A fancy web like a rose

0개의 댓글