- Keywords
simplest memory DS, same data type, modified objects
Creating and Initializing
1) use new
keyword
2) []
Adding and removing elements
1) push()
: add to the end
2) unshift()
: add to the front
// add code
3) pop()
: remove from the back
4) shift()
: remove from the front
//manually code
5) splice()
: remove in between / add in between
*note:
push & pop is an emulation of stack
. shift & unshift is an emulation of queue
Two-dimensional and multi-dimensional arrays
- We might want to use
matrix
(Two-dimensional array
) in some cases. - JS does not support matrices.
- Instead, we use arrays of arrays.
2 x 2
|
|
3 x 3
|
|
Joining multiple arrays
1) concat()
: allows joining multiple arrays and objects into one aray.
*note : works regardless types
|
|
Iterator functions
|
|
1) every()
: iterates each element, passes it to a function, stops when result is false.
2) some()
: iterates each element, passes it to a function, stops when result is true.
3) map()
: Returns an array that stores result of every function run.
4) filter()
: returns an array with elements of true
value.
5) reduce(function(prevValue, currentValue, currentIndex, arr), initialValue)
: returns a result that has been ran continuously with given function through each element.
*note: prevValue == 전 회차에서 반환된 값.
See here for more detail.
Searching and Sorting
1) reverse()
: literally reverses the order of elements.
2) sort()
: sorts elements of an array. This sort is lexicographic, which means by string
, not by numbers
.
If we want our sort()
to sort numerically, we need to pass another function to the sort()
function. This is called compareFunction
.
array.sort(compareFunction)
To sort with ascending order:
To sort with descending order:
- Custom sorting
We can use compareFunction
to compare any type of elements as we define.
- Sorting string
sort()
will sort strings according to ASCII
value.
But sometimes, you would want them to be ordered with different ordering rule.
|
|
- Searching
1) indexOf()
: first match
2) lastIndexOf()
: last match
Outputting the array into a string
1) toString()
: convert array to string, with ,
separator.
2) join()
: do the same thing with toString()
, but with given separator.