: 배열 타입 확인 방법
let num = 123.456;
let str = "Here I am";
let fruits = ["apple", "orange", "melon"];
console.log(Array.isArray(num)); //output: false
console.log(Array.isArray(str)); //output: false
console.log(Array.isArray(fruits)); //output: true
: 배열 일부 요소 삭제
let fruits = ["apple", "orange", "melon"];
console.log(fruits); //output : ['apple', 'orange', 'melon']
console.log(fruits.length); //output : 3
delete fruits[1];
console.log(fruits); //output : ['apple', <1 empty item>, 'melon']
console.log(fruits.length); //output : 3
: 배열 추가
: 배열 삭제
let fruits = ["apple", "orange", "melon"];
ret fruits.push("watermemlon");
console.log(fruits); //output : ['apple', 'kiwi', 'melon', 'watermelon']
console.log(ret); //output : 4
ret = fruits.unshift("watermelon");
console.log(fruits); //output : ['watermelon', 'orange', 'melon']
console.log(ret); //output : 3
ret = fruits.pop();
console.log(fruits); //output : ['apple', 'kiwi', 'melon']
console.log(ret); //output: watermelon
ret = fruits.shift();
console.log(fruits); //output : ['orange', 'melon']
console.log(ret); //output : apple
: 배열 요소 삭제 / 변경 (index는 필수)
let fruits = ["apple", "orange", "melon"];
console.log(fruits.splice(1)); //output : ['orange', 'melon']
console.log(fruits); //output : ['apple']
fruits = ["apple", "orange", "melon", "strawberry"];
console.log(fruits.splice(1 , 1)); //output : ['orange']
console.log(furits); //output : ['apple', 'melon', 'strawberry']
console.log(fruits.splice(1 , 1, "mango", "kiwi")); //output : ['melon']
console.log(fruits); //output : ['apple', 'mango', 'kiwi', 'strawberry']
: 배열 요소 삭제
: 다중 배열 병합
let fruits = ["apple", "orange", "melon"];
console.log(fruits.slice(1)); //output : ['orange', 'melon']
console.log(fruits); //output : ['apple', 'orange', 'melon']
console.log(fruits.slice(1 , 2)); //output : ['orange']
console.log(fruits.slice(-2)); //output : ['orange', 'melon']
// slice 요소는 원본 배열은 그대로
let fruits = ["apple", "orange", "melon"];
console.log(fruits.concat("strawberry"));
//output : ['apple', 'orange', 'melon', 'strawberry']
console.log(fruits.concat(["cherry", "banana"]));
//output : ['apple', 'orange', 'melon', 'cherry', 'banana']
console.log(fruits.concat(["cherry", "banana"], "mango"]
//output : ['apple', 'orange', 'melon', 'cherry', 'b
for문 사용 (index)
//output : apple, orange, melon
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
for ...(element) of
//output : apple, orange, melon
for (let fruit of fruits) {
console.log(fruit);
}
for ...(key) in
//output : apple, orange, melon
for (let key in fruits) {
console.log(fruits[key]);
}
Array.indexOf(item, from)
: index 탐색(앞에서부터)
Array.lastIndexOf(item, from)
: index 탐색(뒤에서부터)
Array.includes(item, from)
: 값 포함 여부 확인
let fruits = ["apple", "orange", "banana", "orange", "melon"];
console.log(fruits.indexOf("orange")); //output : 1
console.log(fruits.indexOf("Orange")); //output : -1
console.log(fruits.indexOf("orange", 2)); //output : 3
console.log(fruits.lastIndexOf("orange")); //output : 3
console.log(fruits.lastIndexOf("orange", -3)); //output : 1
console.log(fruits.lastIndexOf("orange", 0)); //output : -1
console.log(fruits.includes("banana")); //output : true
console.log(fruits.includes("Banana")); //output : false
console.log(fruits.includes(0)); //output : false
: 배열 정렬
: 배열 반전
let nums = [1, -1, 4, 5, 2, 0];
console.log(nums.sort()); //output : [-1, 0, 1, 2, 4, 5]
console.log(nums.reverse()); //output : [5, 4, 2, 1, 0, -1]
let fruits = ["apple", "orange", "banana", "melon"];
console.log(fruits.sort()); //output : ['apple', 'banana', 'melon', 'orange']
console.log(fruits.reverse()); //output : ['orange', 'melon', 'banana', 'apple']
// 고차함수를 이용한 정렬 수행 가능
[before]
let nums = [1, -1, 4, 0, 10, 20, 12];
console.log(nums.sort()); //output : [-1, 0, 1, 10, 12, 20, 4]
console.log(nums.reverse()); //output : [4, 20, 12, 10, 1, 0, -1]
[after]
let ascending_order = function (x , y) {return x - y;};
let descending_order = function (x , y) {return y - x;};
let nums = [1, -1, 4, 0, 10, 20, 12];
console.log(nums.sort(ascending_order)); //output : [-1, 0, 1, 4, 10, 12, 20]
console.log(nums.sort(descending_order)); //output : [20, 12, 10, 4, 1, 0, -1]
: 배열 값을 문자열로 변환
let fruits = ["apple", "orange", "banana", "melon"];
let str = fruits.join();
console.log(str); //output : apple, orange, banana, melon
let str_separator = fruits.join(";");
console.log(str_separator); //output : apple;orange;banana;melon
let result = str_separator.split(";");
console.log(result); //output : ['apple', 'orange', 'banana', ' melon']
: 배열 요소 별 콜백 함수 각각에 실행
(item : 배열 요소, index : 배열 위치, array : 배열)
for문 사용
//output : 1 2 3
let nums = [1, 2, 3];
for (let i = 0; i < nums.length; i++) {
console.log(nums[i]);
}
forEach 사용
//output : 1 2 3
nums.forEach(function (i) {
console.log(i);
});
: 배열 요소 별 함수 호출 및 결과를 배열로 반환
for문 사용
let nums = [1, 2, 3, 4, 5];
let use_for_loop = [];
for (let i = 0; i < nums.length; i++) {
use_for_loop.push(nums[i] * 2);
}
console.log(use_for_loop); //output : [2, 4, 6, 8, 10]
map 사용
let nums = [1, 2, 3, 4, 5];
let use_map = nums.map(function (item) {
return item * 2;
});
console.log(use_map); //output : [2, 4, 6, 8, 10]
: 콜백함수의 조건을 만족하는, 단 하나의 값만 반환
let users = [
{ name : "bob", age : 17, job : false },
{ name : "alice", age : 20, job : false },
{ name : "john", age : 27, job : true },
];
let find_job = users.find(function (user) {
return user.job == false;
});
console.log(find_job);
//output : { name : 'bob', age : 17, job : false }
let find_age = users.find(function (user) {
return user.age > 19;
});
console.log(find_age);
//output : { name : 'alice', age : 20, job : false }
: 요소 별 함수 수행 누적 결과값 반환
let nums = [1, 2, 3, 4, 5];
let call_count = 0;
console.log("result\tvalue\tindex");
let sum = nums.reduce(function (accumulator, item, index, array) {
console.log(accumulator, "\t\t", item, "\t\t", index);
call_count++;
retrun accumulator + item;
}, 0); //initial 없다면 index 1부터 시작
/* output
result value index
0 1 0
1 2 1
3 3 2
6 4 3
10 5 4 */
console.log(call_count); //output : 5
console.log(sum); //output : 15
: 배열 내 단 하나라도 콜백 함수의 조건을 만족하는 요소가 있다면 true, 아니면 false 반환 (빈 배열일 경우 false)
: 배열 내 모든 요소가 콜백 함수의 조건을 만족한다면 true, 아니면 false 반환 (빈 배열일 경우 true)
let users = [
{ name : "bob", age : 17, job : false },
{ name : "alice", age : 20, job : false },
{ name : "john", age : 27, job : true },
];
let some_job = users.some(function (user) {
return user.job == false;
});
console.log(some_job); //output : true
let some_age = users.some(function (user) {
return user.age < 16;
});
console.log(some_age); //output : false
let empty = [].some((item) => item > 16);
console.log(empty); //output : false
let every_job = users.every(function (user) {
return user.job == false;
});
console.log(every_job); //output : false
let every_age = users.every(function (user) {
return user.age < 16;
});
console.log(every_age); //output : true
let empty = [].every((item) => item > 16);
console.log(empty); //output : true
let array = [ [101, 102, 103], [201, 202, 203], [301, 302, 303], ];
console.log(array); //output : [ [101, 102, 103], [201, 202, 203], [301, 302, 303] ]
console.log(array[0]); //output : [ 101, 102, 103 ]
console.log(array[1][0]); //output : 201
console.log(array[2][2]); //output : 303
let arr_2 = array.pop();
console.log(array.length); //output : 2
console.log(arr_2); //output : [ 301, 302, 303 ]
console.log(array); //output : [ [101, 102, 103], [201, 202, 203] ]
let array_num = array.push([401, 402, 403]);
console.log(array.length); //output : 3
console.log(array_num); //output : 3
console.log(array); //output : [ [101, 102, 103], [201, 202, 203], [401, 402, 403] ]
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array[i].length; j++) {
array[i][j] += 1000;
console.log(array[i][j]);
}
}
//output : 1101 ... 1403