100 likes | 118 Views
Discover the power of Javascript arrays and array functions with easy-to-understand CSS examples. Learn how to manipulate arrays effectively using functions like push(), pop(), concat(), and more. Dive into higher-order array functions like forEach and explore the simplicity of arrow functions. See how filter and map functions can enhance your array operations. Take your Javascript skills to the next level with this comprehensive guide.
E N D
Today • Awesome CSS • Arrays
Arrays • Javascript arrays are heterogeneous containers • Javascript arrays may be sparse (please don't do this) • They are initialized either by: • An array literal, e.g. [3, 1, 9] • A call to the Array() function • Accessing array elements can be done using square brackets
Array functions • arr.push() – Adds one or many elements to the end of the array • arr.pop() – Remove and return the last element from the array • arr1.concat(arr2) – Returns a new array containing all elements from arr1 followed by all elements from arr2 • arr.indexOf(elem) – Returns first index where element can be found, or -1 otherwise • arr.reverse() – Reverse array in place • arr.sort() – Sort array in place • …based on converting all values to strings and sorting those.
Higher-order array functions • Javascript arrays have functions that take functions as parameters and use them to do stuff with the array • Simplest example: forEach • arr.forEach(func) – Execute the provided function for each element in the array • Returns undefined as the result • Let's see some examples…
Arrow functions • It's really annoying to have that whole "function(e) { return" stuff for these really simple functions • Arrow function syntax was designed to make short functions easier to write • Let's look at the simplest example…
Filter • arr.filter(func) – Return a new array with all elements from the original array where func(arr) returns true • Seems like you could filter something and then use forEach…
Map • arr.map(func) – Return a new array where each element of the array is the result of calling func(element)