배열 메서드(배열 API)란 무엇이며, Array 생성자 함수의 정적 메서드와 원본을 변경하지 않는 배열 메서드에 대해 적어본다
Array.isArray()
메서드는 전달된 인수가 배열이면 true, 배열이 아니면 false를 반환한다- true를 반환하는 CASE-
const a = Array.isArray([]);
console.log(a) // true
const b = Array.isArray([1,2]);
console.log(b); // true
const c = Array.isArray(new Array());
console.log(c); // true
- false를 반환하는 CASE-
const d = Array.isArray();
console.log(d); // false
const e = Array.isArray({});
console.log(e); // false
const f = Array.isArray(null);
console.log(f); // false
const g = Array.isArray(undefined);
console.log(g); // false
const h = Array.isArray(1);
console.log(h); // false
const i = Array.isArray('Array');
console.log(i); // false
const j = Array.isArray(true);
console.log(j); // false
const k = Array.isArray(false);
console.log(k); // false
const l = Array.isArray({0:1, length:1});
console.log(l); // false
Array.of()
는 Array 생성자 함수의 정적 메서드였다Array.from()
는 Array 생성자 함수의 정적 메서드였다
const arrr = [1, 2, 2, 3]
- 예시1-
arrr.indexOf(2); // 1
arrr.indexOf(4); // -1
- 예시2-
arrr.indexOf(2, 2); // -1
- 예시3-
console.log(arrr); // [1, 2, 2, 3]
const foods = ['apple', 'banana', 'orange']
if(foods.indexOf('mango') == -1) {
foods.push('mango');
}
console.log(foods); // ["apple", "banana", "orange", "mango"]
const numbers = [1, 2, 3, 4]
const fruits = ['Apple', 'Banna', 'Cherry']
let result6 = numbers.concat(fruits)
console.log(reult6); // [1, 2, 3, 4, "Apple", "Banna", "Cherry"]
console.log(numbers.concat(fruits)) // [1, 2, 3, 4, "Apple", "Banna", "Cherry"]
- 원본 배열은 변경되지 않는다-
console.log(numbers) // [1, 2, 3, 4]
console.log(fruits) // ['Apple', 'Banna', 'Cherry']
-예시2-
let result7 = numbers.concat(5)
console.log(result7) // [1, 2, 3, 4, 5]
-예시3-
let result8 = numbers.concat(fruits,5)
console.log(result8) // [1, 2, 3, 4, "Apple", "Banna", "Cherry", 5]
-예시4-
const ahaha = [3, 4]
ahaha.unshift([1,2])
ahaha.push([5,6])
console.log(ahaha) // [[1, 2], 3, 4, [5, 6]]
let result10 = [1,2].concat([3,4])
console.log(result10) // [1, 2, 3, 4]
result10 = result10.concat([5,6])
console.log(result10) // [1, 2, 3, 4, 5, 6]
-예시1-
const mahaha = [1, 2, 3]
let result222 = mahaha.slice(0, 1)
console.log(result222) // [1]
console.log(mahaha) // [1, 2, 3]
-예시2-
let result223 = mahaha.slice(-2)
console.log(result223) // [2, 3]
console.log(mahaha) // [1, 2, 3]
-예시3-
let result224 = mahaha.slice()
console.log(result224) // [1, 2, 3]
console.log(mahaha) // [1, 2, 3]
const mahaha = [1, 2, 3]
let result = mahaha.join() // "1, 2, 3"
console.log(mahaha) // [1, 2, 3]
-예시2-
let result1 = mahaha.join('')
console.log(result1) // "123"
console.log(mahaha) // [1, 2, 3]
-예시3-
let result2 = mahaha.join(' ')
console.log(result2) // "1 2 3"
console.log(mahaha) // [1, 2, 3]
-예시4-
let result3 = mahaha.join(':')
console.log(result3) // "1:2:3"
console.log(mahaha) // [1, 2, 3]
const vha = [1, 2, 3, 4, 5]
let result777 = vha.includes(2)
console.log(result777) // true
console.log(vha) // [1, 2, 3, 4, 5]
-예시2-
const vha = [1, 2, 3, 4, 5]
let result778 = vha.includes(2, 3) // 배열 요소에 2가 포함되어 있는지 index 3부터 검사를 시작한다
console.log(result778) // false
console.log(vha) // [1, 2, 3, 4, 5]
-예시1-
const p = [1, 2, [3, 4, 5]]
let result = p.flat()
console.log(result) // [1, 2, 3, 4, 5]
-예시2-
const p1 = [1, 2, [3, 4, 5]]
let result1 = p1.flat(1)
console.log(result1) // [1, 2, 3, 4, 5]
-예시3-
const p2 = [1, [2, [3, [4, [5]]]]]
let result1 = p2.flat(2)
console.log(result1) // [1, 2, 3, [4, [5]]]
console.log(p2) // [1, [2, [3, [4, [5]]]]]
let result2 = p2.flat(3)
console.log(result2) // [1, 2, 3, 4, [5]]
let result3 = p2.flat(4)
console.log(result3) // [1, 2, 3, 4, 5]
-예시3-
const a = [1, [2, [3, [4, [5]]]]].flat(Infinity)
console.log(a) // [1, 2, 3, 4, 5]