arr === [] // false? - they mean different things, 참조값 .splice .slice 숙지!arr.length !arr - 빈배열 확인하는 방법이 아님 arr === [] : X 이렇게 빈배열 확인하면 안됨! words.indexOf('') is not included in the word array, it spits out -1 words.indexOf('abc') !== -1 to check if it's included (this method is more versatile and offers more info than .includes) words.includes('abc'), but this method isn't supported in IE, whereas indexOf() is!let words = ['Radagast', 'the', 'Brown']
words.indexOf('the') // gives the index of the element
//if it's not included, it spits out '-1' console.table(array) - shows a table with index and value arr.push() -> 뒤에 요소 추가, 추가된 배열의 길이 리턴arr.pop() -> 뒤의 요소 삭제, 제거된 요소 리턴arr.unshift() -> 앞에 요소 추가, 추가된 배열의 길이 리턴arr.shift() -> 앞의 요소 삭제, 제거된 요소 리턴arr.slice()arr.splice(n, 1) -> index n의 요소 하나 삭제, 삭제된 요소 리턴function getAllElementsButNth(arr, n) {
arr.splice(n, 1); //this removes 1 element in index n, and returns that element (does not return an array)
return arr; //this returns the array with the element removed!
}
const restaurants = [“A”, “B”, “C”, “D”]
console.log(restaurants.splice(3, 1)) // output: [“D”]
console.log(restaurants) // output: [“A”, “B”, “C"].slice(): immutablearr.slice(a,b) - b 앞까지만 복사 arr.slice() - 전체 복사, `arr.slice(0, arr.length)랑 똑같음arr.slice(0,-2) - 뒤의 두개 자른 배열을 리턴, arr.slice(0, arr.length-2)와 동일 arr.slice(-1) - 뒤에서부터 셈, 뒤의 1요소만 배열로 리턴 .splice()arr.splice(n, 1);arr.unshift(el) returns length of modified array arr.concat(arr2)arr.concat(9) - 9를 push처럼 배열에 넣어줌 for..of loopfor (let el of arr) {} - el은 arr[i]와 같음. 배열의 모든 요소를 순회하고 싶을 때 유용! for (i =... ) - 다 순회하고 싶지 않을때 for..of문이 불편 for..in loop은 객체에 사용 undefined로 뜸 (아직 배열에 없는 index도 다 임의로 undefined)array a = [1,2,3] 일때 a[3]은 에러 안 뜨고 undefined로 뜸Array.isArray(arr)typeof 역할, boolean array a = [[1,2], [3,4], [5,6]]
// a[1][0] = 3
// a.length = 3at() method: xreplaceAll() method: x undefined + 8 => NaN (Not a Number)
split(), join(), slice(), splice(), Array.isArray(), push(), unshift(), pop(), shift(), indexOf(), includes()