이미 배웠지만 array method가 아직 헷갈린다. 그래서 정리를 해두려고 한다.
먼저 array에서 중요한 점은 array는 ordered collections of values라는 것이다.
한마디로 array에서는 순서가 중요하다.이제 method들을 알아보도록 하자.
let actresses = ["Catherine", "Greta"];
actresses.push("Audrey");
console.log(actresses); // ["Catherine", "Greta", "Audrey"]
actresses.pop(); // "Audrey"
console.log(actresses); // ["Catherine", "Greta"]
actresses.shift(); // "Catherine"
console.log(actresses); // ["Greta"]
actresses.unshift("Eva", "Catherine");
console.log(actresses); // ["Eva", "Catherine", "Greta"]
let dogs = ["Zzong", "Happy"];
let cats = ["Blue", "Baby"];
let animals = dogs.concat(cats);
console.log(animals); // ["Zzong", "Happy", "Blue", "Baby"]
console.log(dogs.includes("Happy")); // true
console.log(dogs.includes("Zzong")); // true
console.log(dogs.includes("Love")); // false
let combo = dogs.concat(cats);
console.log(combo.indexOf("Blue")); // 2
console.log(combo.indexOf("Happy")); // 1
let words = ['Hello', 'world'];
let sentence = words.join(" ");
console.log(sentence); // "Hello world"
combo.reverse();
console.log(combo); // ["Baby", "Blue", "Happy", "Zzong"]
actresses.slice(2, 4); // ["Eva", "Grace"]
console.log(actresses.slice(2)); // ["Eva", "Grace", "Lauren"]
console.log(actresses.slice(-2)); // ["Grace", "Lauren"]
actresses.splice(3, 1); // ["Grace"]
console.log(actresses); // ["Catherine", "Greta", "Eva", "Lauren"]
actresses.splice(1, 0, "Victoria");
console.log(actresses); // ["Catherine", "Victoria", "Greta", "Eva", "Lauren"]
let numbers = [5, 3, 7, 1, 9];
numbers.sort();
console.log(numbers); // [1, 3, 5, 7, 9]
push - add to end
pop - remove from end
shift - remove from start
unshit - add to start
concat - merge arrays
includes- look for a value
indexOf - just like string.indexOf
join- creates a string from an array
reverse - reverses an array
slice- copies a portion on an array
splice - removes/replaces elements
sort- sorts an array