1. .at() : ( arrName.at(정수요소) )
정수 값을 받아, 배열에서 해당하는 인덱스의 요소 반환
ex )
var car = [ '소나타', 50000, 'white' ];
var index = 0;
console.log(`Car name is ${car.at(index)}`)
2. .concat()
배열을 합쳐서 새로운 배열 반환
ex )
var car = [ '소나타', 50000, 'white' ];
var car1 = [ '아반떼', 40000, 'black' ];
var carArr = car.concat(car1);
console.log(carArr);
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);
console.log(b);
4. .sort()
배열을 정렬, ( 숫자 > 영어 > 한글 )
ex )
var car = [ '소나타', 50000, 'white' ];
car.sort();
5. .fill()
배열의 시작 인덱스부터 끝 인덱스의 이전까지 정적인 값 하나로 채움
ex )
const array1 = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4));
console.log(array1.fill(5, 1));
console.log(array1.fill(6));
6. .filter()
주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환
ex )
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
7. .find()
주어진 판별 함수를 만족하는 첫 번째 요소의 값을 반환합니다.
그런 요소가 없다면 undefined를 반환
ex )
const array2 = [5, 12, 8, 130, 44];
const found1 = array2.find(element => element > 10);
console.log(found1);
const found2 = array2.find(element => element > 140);
console.log(found2);
8. .findIndex()
주어진 판별 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스를 반환
만족하는 요소가 없으면 -1을 반환
ex )
const array3 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array3.findIndex(isLargeNumber));
const isLargeNumber = (element) => element > 140;
console.log(array3.findIndex(isLargeNumber));
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'));
console.log(beasts.indexOf('bison', 2));
console.log(beasts.indexOf('giraffe'));
11. .lastIndexOf()
배열에서 주어진 값을 발견할 수 있는 마지막 인덱스를 반환
존재하지 않으면 -1을 반환
ex )
var array = [2, 5, 9, 2];
array.lastIndexOf(2);
array.lastIndexOf(7);
array.lastIndexOf(2, 3);
array.lastIndexOf(2, 2);
array.lastIndexOf(2, -2);
array.lastIndexOf(2, -1);
12. .join()
배열의 모든 요소를 연결해 하나의 문자열
ex )
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
console.log(elements.join(''));
console.log(elements.join('-'));
13. .map()
배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환
ex )
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);
14. .pop()
배열에서 마지막 요소를 제거하고 그 요소를 반환
ex )
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
console.log(plants);
plants.pop();
console.log(plants);
15. .push()
배열의 끝에 하나 이상의 요소를 추가하고, 배열의 새로운 길이를 반환
ex )
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
console.log(animals);
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
16. .reduce()
배열의 각 요소에 대해 주어진 리듀서(reducer) 함수를 실행하고, 하나의 결과값을 반환
ex )
const array1 = [1, 2, 3, 4];
const initialValue = 0;
const sumWithInitial = array1.reduce(
(previousValue, currentValue) => previousValue + currentValue,
initialValue
);
console.log(sumWithInitial);