[ ];
//변수에 배열 리터럴 할당
const arr = ['zero', 'one', 'two', 'three'];
//변수에 객체 리터럴 형식의 배열
const obj = {'0': 'zero', '1': 'one', '2': 'two','3': 'three'}
//매개변수 1개일 때
const arr = new Array(2);
console.log(arr); // (2) [empty × 2]
//그 외
const arr = new Array(1, 2, 3);
console.log(arr); // [1, 2, 3]
delete
연산자 사용//배열 리터럴
const fruits = ['🍌', '🍎', '🍇', '🍑'];
// 배열 아이템을 참조하는 방법
console.log(fruits[0]); //🍌
console.log(fruits[1]); //🍎
console.log(fruits[2]); // 🍇
console.log(fruits[3]); //🍑
//배열객체의프로퍼티
console.log(fruits.length); //4
// 좋지 않은 방식 💩
//추가 - 인덱스를 이용해서 추가하는 것은 좋지 않은 방식
fruits[6] = '🍓';
console.log(fruits); //[ '🍌', '🍎', '🍇', '🍑', <2 empty items>, '🍓' ]
//삭제 - 삭제를 해도 텅빈 배열이 남겨져서 안좋음💩
delete fruits[1];
console.log(fruits); //[ '🍌', <1 empty item>, '🍇', '🍑', <2 empty items>, '🍓' ]
forEach(),for문,for..of문
static(= 클래스레벨)
this(원본배열 =인스턴스레벨)
변경this(원본배열 =인스턴스레벨)
변경하지 않음static
정적 메서드는 인수가 배열이면 true, 배열이 아니라면 false// true
Array.isArray([]);
Array.isArray([1, 2]);
Array.isArray(new Array());
// false
Array.isArray();
Array.isArray({});
// 문자열은 이터러블이다.
const arr1 = Array.from('Hello');
console.log(arr1); // [ 'H', 'e', 'l', 'l', 'o' ]
// 유사 배열 객체를 새로운 배열을 변환하여 반환한다.
const arr2 = Array.from({ length: 2, 0: 'a', 1: 'b' });
console.log(arr2); // [ 'a', 'b' ]
// Array.from의 두번째 매개변수에게 배열의 모든 요소에 대해 호출할 함수를 전달할 수 있다.
const arr3 = Array.from({ length: 5 }, function (v, i) { return i; });
console.log(arr3); // [ 0, 1, 2, 3, 4 ]
const arr = [1, 2, 2, 3];
arr.indexOf(2); // -> 1
arr.indexOf(2, 2); // 2
arr.indexOf(4); // -1
const foods = ['apple', 'banana'];
// foods 배열에 'orange' 요소가 존재하는지 확인
if (foods.indexOf('orange') === -1) {
// foods 배열에 'orange' 요소가 존재하지 않으면 'orange' 요소를 추가
foods.push('orange');
}
console.log(foods); //[ 'apple', 'banana', 'orange' ]
const arr = ['hello','hi']
if(!arr.includes('bye')){
arr.push('bye');
}
console.log(arr); //['hello','hi','bye']
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1.concat(arr2);
const result = arr2.concat(3);
console.log(arr1); //[ 1, 2, 3 ]
console.log(arr2); //[ 4, 5, 6 ]
console.log(arr3); //[ 1, 2, 3, 4, 5, 6 ]
console.log(result); //[4,5,6,3]
const arr=[1,2];
let result = arr.push(0,5);
console.log(result); // 4
console.log(arr); //[1,2,0,5]
const arr1 = [1, 2];
// 인수로 전달받은 배열을 그대로 원본 배열의 마지막 요소로 추가한다
arr1.push([3, 4]);
console.log(arr1); // [1, 2, [3, 4]]
const arr2 = [1, 2];
// 인수로 전달받은 배열을 해체하여 새로운 배열의 마지막 요소로 추가한다
const result = arr2.concat([3, 4]);
console.log(result); // [1, 2, 3, 4]
// 스택 자료 구조를 구현하기 위한 배열
const stack = [];
// 스택의 가장 마지막에 데이터를 밀어 넣는다.
stack.push(1);
console.log(stack); // [1]
// 스택의 가장 마지막에 데이터를 밀어 넣는다.
stack.push(2);
console.log(stack); // [1, 2]
// 스택의 가장 마지막 데이터, 즉 가장 나중에 밀어 넣은 최신 데이터를 꺼낸다.
let value = stack.pop();
//제거한 요소 반환, 제거되어진 스택 배열
console.log(value, stack); // 2 [1]
// 스택의 가장 마지막 데이터, 즉 가장 나중에 밀어 넣은 최신 데이터를 꺼낸다.
value = stack.pop();
console.log(value, stack); // 1 []
const a = ['a','b','c'];
const b =a.reverse();
console.log(a); // [ 'c', 'b', 'a' ]
console.log(b); // [ 'c', 'b', 'a' ]
const a =['a','b','c'];
const b = a.shift();
console.log(a); //['b','c']
console.log(b); //'a'
const a =['a','b','c'];
const b = a.unshift(3);
console.log(a); //[3,'a','b','c']
console.log(b); //4
arr.slice( start [, deleteCount, items])
start
: 배열의 start 위치부터 모든 요소를 제거deleteCount
: 시작위치부터 제거할 요소의 수, 0인경우 아무런 요소 제거되지 않음items
: 삭제한 위치에 추가될 요소, 아무런 요소 지정하지 않을 경우 삭제만 됨const items1 = [1, 2, 3, 4];
//제거 ( start, deleteCount )
const res1 = items1.splice(1, 2);
console.log(items1); // [ 1, 4 ]
console.log(res1); // [ 2, 3 ]
//제거 및 추가 (start, deleteCount, items )
const items = [1, 2, 3, 4];
const res = items.splice(1, 2, 20, 30);
console.log(items); // [ 1, 20, 30, 4 ]
console.log(res); // [ 2, 3 ]
//추가 ( start , delteCount=0, items)
const items = [1, 2, 3, 4];
const res = items.splice(1, 0, 100);
console.log(items); // [ 1, 100, 2, 3, 4 ]
// 제거한 요소가 배열로 반환된다.
console.log(res); // [ ]
arr.slice(start [, end]
)start
: 복사를 시작할 인덱스 , 음수인 경우 배열의 끝에서의 인덱스end
: 옵션이며 기본값은 length값const items = ['a', 'b', 'c'];
let res = items.slice(0, 1);
console.log(res); // [ 'a' ]
res = items.slice(1, 2);
console.log(res); // [ 'b' ]
res = items.slice(1);
console.log(res); // [ 'b', 'c' ]
res = items.slice(-1);
console.log(res); // [ 'c' ]
res = items.slice(-2);
console.log(res); // [ 'b', 'c' ]
// 모든 요소를 반환 (= 복사본(shallow copy) 생성)
res = items.slice();
console.log(res); // [ 'a', 'b', 'c' ]
// ✨✨✨✨원본은 변경되지 않는다.
console.log(items); // [ 'a', 'b', 'c' ]
console.log( res === items ) //false
배열을 아무리 복사해서 수정해도 그 안의 오브젝트 프로퍼티 값은 변하지 않는다 = 원본은 변하지 않는다.
왜냐?
💡얕은복사가 일어났기 때문~ 그게 뭐임
: 오브젝트 자체가 저장된것이 아닌 오브젝트가 지정된 메모리 주소를 복사한 것이다.
=> 즉, 복사된 배열들은 서로 다른 배열의 메모리지만
그 안의 오브젝트 프로퍼티 값은 같은 메모리 주소인 것.
,
const arr = [1, 2, 3, 4];
let result = arr.join();
console.log(result); // '1,2,3,4';
// ✨원본 배열 arr의 모든 요소를 문자열로 변환한 후, 빈문자열로 연결한 문자열을 반환
result = arr.join('');
console.log(result); // '1234'
// 중첩 배열을 하나의 배열로 쫙 펴기
let arr = [[1, 2, 3],[4, [5, 6, [3, 4]]],];
console.log(arr); //[ [ 1, 2, 3 ], [ 4, [ 5, 6, [Array] ] ] ]
console.log(arr.flat(3));
// [
// 1, 2, 3, 4,
// 5, 6, 3, 4
// ]
result = arr.flat(1);
console.log(result); //[ 1, 2, 3, 4, [ 5, 6, [ 3, 4 ] ] ]
arr.fill(items, start, end)
arr=[1,2,3,4,5,6];
arr.fill(0); // 특정한 값으로 수정
console.log(arr);
// [
// 0, 0, 0, 0,
// 0, 0, 0, 0
// ]
arr.fill('s', 1, 3);//원하는 인덱스 구간 수정
console.log(arr);
// [
// 0, 's', 's', 0,
// 0, 0, 0, 0
// ]
arr.fill('a', 1);//시작인덱스부터 끝까지 수정
console.log(arr);
// [
// 0, a, a, a,
// a, a, a, a
// ]
,
을 구분자로 사용const arr =[1,2,3,'0'];
let text = arr.join();
console.log(text); //1,2,3,0
text = arr.join(' | ');
console.log(text); //1 | 2 | 3 | 0
console.log(typeof text); //string
console.log(typeof text[0]); //string