on
17. JavaScript Arrays
17. JavaScript Arrays
* Udemy "The Web Developer Bootcamp 2021"(by. Colt Steele)의 강의내용을 정리
175. Introducing Arrays
- Array is an oredered collection of values. (ex. list of comments in post, collection of level in a game)
- .length property also works in array.
// To make an empty array let students = []; // An array of string let colors = ['yellow', 'red', 'blue']; // A mixed array let stuff = [true, 68, 'cat', null] stuff.length // 4
176. Array Random Access
- Each element has a corresponding index (counting starts at 0), works same as string.
let stuff = [true, 68, 'cat', null] stuff[0] // true stuff[1] // 68 stuff[3] // null sutff[4] // undefined
- You can alter the element by approaching array with index. (String is not available for this case.)
- You can make blanks between the elements inside same array.
let color = ["rad", "yellow", "green"] color[0] = "red" // actually changes the first element of color to "red" (const also changes) let name = "Kim" name[0] = "L" // name is still "kim", not "Lim" color[10] = 10 // color = ["red", "yellow", "green", emptyX7, 10]
177. Push & Pop
- Push is a method to add elements at the end of an array.
- Pop is a method to remove elements from the end of an array. (Pop does not require any argument.)
let num = [1, 2, 3, 4] num.push(5) // [1,2,3,4,5] num.push() // [1,2,3,4]
178. Shift & Unshift
- Shift is a method to remove elements from the start of an array. (Shift does not require any argument.)
- Unshift is a method to add elements at the start of an array.
let num = [1, 2, 3, 4] num.shift() // [2,3,4] num.unshift(0) // [0,2,3,4]
179. Concat, indexOf, includes & reverse
Methods What do thye do? 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
let cats = ["blue", "kitty"] let dogs = ["rusty", "wyatt"] //concat - merges arrays cats.concat(dogs) // ["blue", "kitty", "rusty", "wyatt"] // doesn't change cats itself console.log(cats) // ["blue", "kitty"] //includes - looks for a value cats.includes("blue") // true cats.includes("kitti") // false //reverse - reverse an array cats.reverse() // ["kitty", "blue"]
180. Slice & Splice
- Negative argument in slice() means last elements. (ex. slice(-2) means last two elements in the array)
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']; console.log(animals.slice(2)); // ["camel", "duck", "elephant"] console.log(animals.slice(1, 5)) // ["bison", "camel", "duck", "elephant"] console.log(animals.slice(-2)) // ["duck", "elephant"] console.log(animals.slice(2, -1)) // ["camel", "duck"]
const months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); // inserts at index 1 console.log(months); // ["Jan", "Feb", "March", "April", "June"] months.splice(4, 1, 'May'); // replaces 1 element at index 4 console.log(months); // ["Jan", "Feb", "March", "April", "May"]
182. Array + Const
- Array is a type of object, so it has reference in the memory. For the reference remains the same, the array does not change.
const num = [1,2,3,4] nums = 1 // Type error! nums = [1,2,3,4] // Type error! It looks same, but it is different reference in JS eye nums.push(5) console.log(nums) // [1,2,3,4,5] You can change the inside as long as the reference is unchanged
183. Multi-Dimensional Array
- We can store arrays inside the other array. (nested arrays)
const backNum = [ [9, 10, 11], [6, 13, 20], [2, 3, 4] ] backNum[0] // [9,10,11] backNum[0][2] // 11
반응형
from http://oaat9309.tistory.com/34 by ccl(A) rewrite - 2021-11-09 11:01:15