console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]
console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]
console.log(Array.from({ a: '1', b: '2' }));
// [] : object는 빈 배열로 리턴된다.
let obj = { a: '1', b: '2' };
Array.from(Object.keys(obj)); // key값 리턴
// ["a", "b"]
Array.from(Object.values(obj)); // value값 리턴
// ["1", "2"]
Array.of(7); // [7]
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); // [undefined]
Array(7); // [ , , , , , , ]
Array(1, 2, 3); // [1, 2, 3]
const alpha = ['a', 'b', 'c'];
const numeric = [1, 2, 3];
alpha.concat(numeric);
// 결과: ['a', 'b', 'c', 1, 2, 3]
let b = [1, 2, 3];
let c = [4, 5, [6, 7]];
b.concat(c);
// [1, 2, 3, 4, 5, [6, 7]]
var array1 = [1, 2, 3, 4];
// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]
// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]
console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(words);
console.log(result);
// Array ["spray", "limit", "elite", "exuberant", "destruction", "present"]
// Array ["exuberant", "destruction", "present"]
var array1 = [5, 12, 8, 130, 44];
var found = array1.find(function(element) {
return element > 10;
});
console.log(found);
// expected output: 12
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
const result = inventory.find(fruit => fruit.name === 'cherries');
console.log(result) // { name: 'cherries', quantity: 5 }
// array length is 3
// fromIndex is -1
// computed index is 3 + (-1) = 2
var arr = ['a', 'b', 'c'];
arr.includes('a', -1); // false
arr.includes('a', -2); // false
arr.includes('a', -3); // true
var ary = [
{ a: 'a', b: 'b'},
{ a: 'aa', b: 'bb'}
];
ary.includes({ a: 'a', b: 'b'}); // false. 객체는 확인 불가
keys() 메서드는 배열의 각 인덱스를 키 값으로 가지는 새로운 Array Iterator 객체를 반환한다.
var array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1); // expected output: Array [2, 8, 18, 32]
console.log(array1); // Array [1, 4, 9, 16] 원본 유지
// 아래 라인을 보시면...
['1', '2', '3'].map(parseInt);
// 결과를 [1, 2, 3] 으로 기대할 수 있습니다.
// 그러나 실제 결과는 [1, NaN, NaN] 입니다.
// parseInt 함수는 보통 하나의 인자만 사용하지만, 두 개를 받을 수 있습니다.
// 첫 번째 인자는 변환하고자 하는 표현이고 두 번째는 숫자로 변환할 때 사용할 진법입니다.
// Array.prototype.map은 콜백에 세 가지 인자를 전달합니다.
// 배열의 값, 값의 인덱스, 그리고 배열
// 세 번째 인자는 parseInt가 무시하지만 두 번째 인자는 아닙니다.
[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array) {
return accumulator + currentValue;
});
var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
const ary1 = animals.slice(2);
const ary2 = animals.slice(2, 4)
console.log(animals);
console.log(ary1);
console.log(ary2);
animals[0] = 'aaaaa';
console.log(animals);
console.log(ary1);
// Array ["ant", "bison", "camel", "duck", "elephant"]
// Array ["camel", "duck", "elephant"]
// Array ["camel", "duck"]
// Array ["aaaaa", "bison", "camel", "duck", "elephant"]
// Array ["camel", "duck", "elephant"]
var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
months.splice(1, 2, 'May');
console.log(months);
// expected output: Array ["Jan", "May", "April", "May"]
var array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// expected output: "1,2,a,1a"
var array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// expected output: 5
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]
var arr = [1, 2];
arr.unshift(0); // result of call is 3, the new array length
// arr is [0, 1, 2]
arr.unshift(-2, -1); // = 5
// arr is [-2, -1, 0, 1, 2]
arr.unshift([-3]);
// arr is [[-3], -2, -1, 0, 1, 2]