.push(); 메서드

기존 배열에 새로운 값 추가

let fruits = ['apple', 'banana'];

// 배열에 'orange' 추가
fruits.push('orange');

console.log(fruits); // ['apple', 'banana', 'orange']
// 콘솔창에서 'apple', 'banana', 'orange' 출력한다.

 

findIndex(() => ); 메서드

입력한 값의 인덱스를 찾음

let numbers = [10, 20, 30, 40, 50];

// 값이 30인 요소의 인덱스를 찾음
let index = numbers.findIndex((num) => num === 30);

console.log(index); 
// 콘솔창 결과값 : 2

 

map(() => ); 메서드

해당 배열의 모든 엘리먼트를 다른 엘리먼트로 변환시켜줌

let numbers = [1, 2, 3, 4, 5];

// 배열의 각 요소에 2를 곱한 값으로 새로운 배열을 만듦
let doubled = numbers.map((num) => num * 2);

console.log(doubled); 
// 콘솔창 결과값 : [2, 4, 6, 8, 10]

 

'JS' 카테고리의 다른 글

스프레드 연산자 (...)  (0) 2024.09.20
객체 분해 (디스트럭처링)  (0) 2024.09.19
JavaScript  (0) 2024.09.12
Import & Export  (0) 2024.09.11
Script 사용법 그리고 속성들  (0) 2024.09.10

+ Recent posts