# INDEX
1. Values & Data type
2. Operators
3. Control flow
4. Scope & Hoisting
5. Object & Array
6. This
7. Prototype & Inheritance
8. Function
9. Callback function
10. Closuer
11. Class
12. Others
해당 포스팅은 MDN의 내용을 참고하였습니다
Array.from (arrayLike [, mapFn [, thisArg]] ) |
---|
arrayLike : 배열로 변환하고자 하는유사 배열 객체나 반복 가능한 객체 mapFn(opt) : 배열의 모든 요소에 대해 호출할 맵핑 함수 thisArg(opt) : mapFn 실행 시에 this로 사용할 값 |
return : 새로운 Array 인스턴스 |
Array.from('foo'); // ["f", "o", "o"]
------------------------------------------
const foo = new Set(['foo', window]);
Array.from(foo); // ["foo", window]
------------------------------------------
const mapper = new Map([['1', 'a'], ['2', 'b']]);
Array.from(mapper.values()); // ['a', 'b'];
Array.isArray (obj) |
---|
obj : 판별할 객체 |
return : if 객체가 Array이면 true, 아니면 false |
return true ex)
Array.isArray(new Array('a', 'b', 'c', 'd'));
Array.isArray(new Array(3));
Array.isArray(Array.prototype);
------------------------------------------
return false ex)
Array.isArray();
Array.of (element0 [, element1 [, ... [, elementN] ] ] ) |
---|
elementN : 배열을 생성할 때 사용할 요소 |
return : 새로운 Array 객체 |
Array.of(1); // [1]
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); // [undefined]
array.concat ( [value1 [, value2 [, ... [, valueN] ] ] ] ) |
---|
매개변수 : 배열 or 값 valu1..valueN 인자를 생략하면 기존배열의 얕은 복사본을 반환함 |
return : 새로운 Array 객체 |
let num1 = [1];
let num2 = {name : "willy"}
let num3 = [7, 8, 9];
let add1 = num1.concat();
console.log(d) // [1]
let add2 = [num1[0]].concat(num2);
console.log(e) // [1, {name: "willy"}]
console.log(add2[1]) // [object Object] {name: "willy"}
arr.filter ( callback ( element [, index [, array]] ) [, thisArg] ) |
---|
callback : 각 요소를 테스트할 함수 / true를 반환하면 요소 유지, false를 반환하면 종료 / 아래 세 가지 매개변수를 받음 element : 처리할 현재 요소 index(opt) : 처리할 현재 요소의 인덱스 array (opt) : filter를 호출한 배열 thisArg (opt) : callback을 실행할 때 this로 사용하는 값 |
return : 테스트를 통과한 요소만으로 이루어진 새로운 배열 / 어떤 요소도 테스트를 통과하지 못하면, 빈 배열을 반환 |
function isBigEnough(value) {
return typeof value === "boolean";
}
let filtered = [1,2,3,true, {name:"willy"}]
let result = filtered.filter(isBigEnough);
console.log(result); // [true]
arr.forEach ( callback ( currentvalue [, index [, array] ] ) [, thisArg] ) |
---|
callback : 각 요소에 대해 실행할 함수 / 아래 세 가지 매개변수를 받음 currentValue : 처리할 현재 요소 index (opt) : 처리할 현재 요소의 인덱스 array (opt) : forEach()를 호출한 배열 thisArg (opt) : callback을 실행할 때 this로 사용할 값 |
return : undefined |
let items = ['item1', 'item2', 'item3'];
let copy = [];
// for문 사용 ex)
for (let i=0; i<items.length; i++) {
copy.push(items[i]);
}
// forEach 사용 ex)
items.forEach(function(item){
copy.push(item);
console.log(items); // ["item1", "item2", "item3"]
// ["item1", "item2", "item3"]
// ["item1", "item2", "item3"]
});
console.log(copy); // ["item1", "item2", "item3"]
arr.includes ( valueToFind [, fromIndex] ) |
---|
valueToFind : 탐색할 요소 - 문자열을 비교할 때, 대소문자를 구분함 fromIndex (opt) : 해당 배열에서 searchElement 검색을 시작할 위치 - 0이 기본값 - 음수일 경우 : array.length + fromIndex의 인덱스 - 계산된 인덱스가 -1 * array.length 보다 작거나 같다면, 전체 배열이 검색됨 - fromIndex 가 배열의 길이와 같거나 크면, false 를 반환 |
return : Boolean |
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(3, 3); // false (같거나 큰 경우)
[1, 2, 3].includes(3, -1); // true (음수의 경우 : 3 + (-1) === 2 )
[1, 2, 3].includes(3, -1) === [1, 2, 3].includes(3, 2); // true
['a', 'b', 'c'].includes("a", -100)// true (계산된 인덱스가 -1*array.length와 같거나 더 작은 경우)
['a', 'b', 'c'].includes("a", -2); // false
arr.indexOf ( searchElement [, fromIndex ] ) |
---|
searchElement : 배열에서 찾을 요소 fromIndex Optional : 검색을 시작할 인덱스 - 인덱스가 배열의 길이와 같거나 큰 경우 -1이 반환되므로 배열이 검색되지 않음 - 제공된 인덱스가 음수이면 배열은 앞에서 뒤로 검색 - 계산 된 인덱스가 0보다 작으면 전체 배열이 검색됨 - 기본값 : 0 (기본값의 경우, 전체 배열을 검색함) |
return : 배열 내 요소의 첫번째 인덱스값 / 없으면 -1 |
let array = [2, 9, 9];
array.indexOf(2); // 0
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0
------------------------------------------
function updateVegetablesCollection (veggies, veggie) {
if (veggies.indexOf(veggie) === -1) {
veggies.push(veggie);
console.log('새로운 veggies 컬렉션 : ' + veggies);
} else if (veggies.indexOf(veggie) > -1) {
console.log(veggie + ' 은 이미 veggies 컬렉션에 존재합니다.');
}
}
let veggies = ['potato', 'tomato', 'chillies', 'green-pepper'];
updateVegetablesCollection(veggies, 'spinach');
// 새로운 veggies 컬렉션 : potato, tomato, chillies, green-pepper, spinach
updateVegetablesCollection(veggies, 'spinach');
// spinach 은 이미 veggies 컬렉션에 존재합니다.
arr.join ( [ separator ] ) |
---|
separator (opt) : 배열의 각 요소를 구분할 문자열을 지정 |
return : 배열의 모든 요소들을 연결한 하나의 문자열을 반환 / if (arr.length===0)일 경우, 빈 문자열을 반환 |
let baby = ["willy", "is", "so", "cute"];
console.log(baby.join()); // "willy,is,so,cute"
console.log(baby.join("")); // "willyissocute"
console.log(baby.join("-")); // "willy-is-so-cute"
arr.map ( callback ( currentValue [, index [, array] ] ) [, thisArg] ) |
---|
callback : 새로운 배열 요소를 생성하는 함수 / 아래 세 가지 인수를 가질 수 있음 - currentValue : 처리할 현재 요소 - index (opt) : 처리할 현재 요소의 인덱스 - array (opt) : map()을 호출한 배열 thisArg Optional : callback을 실행할 때 this로 사용되는 값 |
return : 배열의 각 요소에 대해 실행한 callback의 결과를 모은 새로운 배열 |
let number = [1, 4, 9];
let doubles = number.map(function(num) {
return num * 2;
});
console.log(doubles); // [2, 8, 18]
console.log(number); // [1, 4, 9] (원본 배열 유지)
---------------------------------------------
let array = [{key:1, value:10},
{key:2, value:20},
{key:3, value: 30}];
let reformattedArray = array.map(function(obj){
let newObj = {};
newObj[obj.key] = obj.value;
return newObj;
});
console.log(reformattedArray); // [{1:10}, {2:20}, {3:30}]
arr.pop() |
---|
return : 배열에서 제거한 요소 / 빈 배열의 경우, undefined |
let name = ["jung", "eun", "lee"];
console.log(name.pop()); // "lee"
console.log(name); // ["jung", "eun"]
arr.push ( element1 [, ...[, elementN] ] ) |
---|
elementN : 배열의 끝에 추가할 요소 |
return : 호출한 배열의 새로운 length 속성 |
let baby = ["willy"];
let add = baby.push("is", 2, "years old")
console.log(add); // 4
console.log(baby); // ["willy", "is", 2, "years old"]
arr.shift ( ) |
---|
return : 배열에서 제거한 요소 / 빈 배열의 경우, undefined |
arr.unshift ( [...elementN] ) |
---|
elementN : 배열 맨 앞에 추가할 요소 |
return : 메서드를 호출한 배열의 새로운 length 속성 |
NaN
Infinity
NaN
Infinity
NaN