JavaScript Array

김재민·2022년 2월 14일
0

JavaScript

목록 보기
7/7

Array.prototype.filter()

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result)

//Array : ["exuberant", "destruction", "present"]

Array.prototype.find()

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found)
// expected output : 12

find() 메서드는 주어진 판별 함수를 만족하는 첫 번째 요소 값을 반환. 해당 값이 없다면 undefined를 반환

Array.prototype.concat()

concat() 메서드는 인자로 주어진 배열이나 값들을 기존 배열에 합쳐서 새 배열을 반환

  • 기존배열을 변경하지 않음
  • 추가된 새로운 배열을 반환
const array1 = ['a','b','c'];
const array2 = ['d','e','f'];
const array3 = array1.concat(array2);

console.log(array3);
//expected output : Array["a", "b", "c", "d", "e", "f"]

Array.prototype.indexOf()

indexOf() 메서드는 배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환하고 존재하지 않으면 -1 을 반환합니다.

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

console.log(beats.indexOf('bison'));
// expected output: 1
let fruits = ['사과', '바나나']

fruits.forEach(function(item, index, array){
	console.log(item, index)
}

//사과 0
//바나나 1

배열 앞에서부터 항목 제거하기
let first = fruits.shift() // 제일 앞의 '사과'를 제거
// ['바나나']

배열 앞에 항목 추가하기
let newLength = fruits.unshift('딸기') // 앞에 추가
// ["딸기","바나나"]

fruits.push('망고')
// ["딸기", "바나나", "망고"]

인덱스 위치에 있는 항목 제거

let removedItem = fruits.splice(1, 1) //항목을 제거
//["딸기", "망고"]



profile
어제의 나보다 나은 오늘의 내가 되자!🧗‍♂️

0개의 댓글

관련 채용 정보