const like = {0:"zero", 1:"one", length:2};
const list = Array.from(like);
log(list);
log(Array.from("ABC"));
// [zero, one]
// [A, B, C]
function args(){
return Array.from(arguments);
};
log(args(1, 2, 3));
// [1, 2, 3]
// <li class=sports>축구</li>
// <li class=sports>농구</li>
const nodes = document.querySelectorAll(".sports");
const show = (node) => log(node.textContent);
Array.from(nodes).forEach(show);
// 축구
// 농구
const like = {0:"zero", 1:"one", length:2};
log(Array.from(like, value => {
return value + "변경";
}));
// [zero변경, one변경]
const like = {0: 10, 1: 20, length:2};
log(Array.from(like, function(value){
return value + this.plus;
}, {plus: 70}));
// [80, 90]
const result = Array.of(1, 2, 3);
log(result);
log(Array.of());
// [1, 2, 3]
// []
범위 값을 복사하여 같은 오브젝트에 설정
두 번째 파라미터의 인덱스부터 복사하여
const list = ["A", "B", "C", "D", "E"];
const copy = list.copyWithin(1, 3);
log(list);
log(copy);
// [A, D, E, D, E]
// [A, D, E, D, E]
const list = ["A", "B", "C", "D", "E"];
list.copyWithin(0, 2, 4);
log(list);
// [C, D, C, D, E]
const list = ["A", "B", "C", "D", "E"];
list.copyWithin(3);
log(list);
// [A, B, C, A, B]
const list = ["A", {B: "가"}, "C"];
log(list.copyWithin(0, 1));
// [{B: 가}, C, C]
스펙에서 아래 문장을 볼 수 있음
generic 사용 형태
const like = {0: 10, 1: 20, 2: 30, length: 3};
log(Array.prototype.copyWithin.call(like, 1, 0));
// {0: 10, 1: 10, 2: 20, length: 3}
const list = ["A", "B", "C"];
const cb = (value, index, all) => value === "B";
const result = list.find(cb);
log(result);
// B
const list = ["A", "B", "C"];
const cb = (value, index, all) => value === 77;
const result = list.find(cb);
log(result);
// undefined
const list = ["A", "B", "C"];
function cb(value, index, all){
return value === "A" && value === this.check;
};
const result = list.find(cb, {check:"A"});
log(result);
// A
const list ["A", "B", "C"];
const cb = (value, index, all) => value === "B";
log(list.findIndex (cb));
// 1
cosnt list = ["A", "B", "C"];
const cb = (value, index, all) => value === 77;
const result = list.findIndex(cb);
log(result);
// -1
범위 값을 지정한 값으로 설정, 반환
설정 방법
const list = ["A", "B", "C"];
list.fill("책", 1);
log(list);
// [A, 책, 책]
const list = ["A", "B", "C", "D"];
list.fill("책", 1, 3);
log(list);
// [A, 책, 책, D]
const list = ["A", "B", "C"];
list.fill("책");
log(list);
// [책, 책, 책]
const like = {0:"A", 1:"B", 2:"C", length: 3};
log(Array.prototype.fill.call(like, "책", 1));
// {0: A, 1: 책, 2: 책, length: 3}
const list = [10, 20, 30];
log(list.includes(10));
log(list.includes(50));
log(list.includes(10, 1));
// true
// false
// false
const like = {0: 10, 1:, 20, 2: 30, length: 3};
log(Array.prototype.includes.call(like, 20));
// true
const list = [1, 2, [3, 4]];
const result = list.flat();
log(result);
log(list);
// [1, 2, 3, 4]
// [1, 2, [3, 4]]
const list = [1, 2, [3, 4]];
log(list.flat(0));
// [1, 2, [3, 4]]
const list = [1, 2, [3, 4, [5, 6]]];
log(list.flat(2));
// [1, 2, 3, 4, 5, [6]]
const list = [1, 2, . . . [3, 4]];
log(list.length);
const change = list.flat();
log(change);
log(change.length);
// 6
// [1, 2, 3, 4]
// 4
flat()와 기본 기능은 같음
배열을 반복하면서 콜백 함수 호출
const list = [10, 20];
const cb = (element, index, all) => {
return element + 5;
};
log(list.flatMap(cb));
log(list.map(cb));
// [15, 25]
// [15, 25]
const list = [10, 20];
const cb = (element, index, all) => {
return [element + 5];
};
log(list.map(cb));
log(list.flatMap(cb));
// [[15], [25]]
// [15, 25]
Array 오브젝트를 Array 이터레이터 오브젝트로 생성, 반환
배열의 엘리먼트를 [key, value] 형태로 변환
const iterator = ["A", "B"].entries();
log(iterator.next().value);
log(iterator.next().value);
// [0, A]
// [1, B]
const iterator = ["A", "B"].entries();
for (const property of iterator) {
log(property);
};
// [0, A]
// [1, B]
// 분할 할당
const iterator = ["A", "B"].entries();
for (const [key, value] of iterator) {
log(`${key}: ${value}`);
};
// 0: A
// 1: B
const iterator = ["A", "B"].entries();
for (const [key, value] of iterator){
log(`${key}: ${value}`);
};
for (const property of iterator){
log("다시 전개");
};
log(iterator.next());
// 0: A
// 1: B
// {value: undefined, done: true}
const iterator = ["A", "B"].keys();
log(iterator.next());
log(iterator.next());
log(iterator.next());
// {value: 0, done: false}
// {value: 1, done: false}
// {value: undefined, done: true}
const iterator = ["A", "B"].keys();
for (const property of iterator) {
log(property);
};
// 0
// 1
Array 오브젝트를 Array 이터레이터 오브젝트로 생성, 반환
[key, value] 형태에서
배열의 엘리먼트 값이 value가 됨
const iterator = ["A", "B"].values();
log(iterator.next());
log(iterator.next());
log(iterator.next());
// {value: A, done: false}
// {value: B, done: false}
// {value: undefined, done: true}
const iterator = ["A", "B"].values();
for (const property of iterator) {
log(property);
};
// A
// B
[Symbol.iterator]()
사용const check = Array.prototype.values === Array.prototype[Symbol.iterator];
log(check);
const iterator = ["A", "B"][Symbol.iterator]();
for (const property of iterator) {
log(property);
};
// true
// A
// B
let list = ["A", "B"];
let iterator = list.values();
list[0] = "연동";
log(iterator.next());
log(iterator.next());
// {value: 연동, done: false}
// {value: B, done: false}