Advance Angular Lecture 2
Advance Angular Lecture 2
17
2. Array Functions - 2
MUTATION FUNCTIONS
Mutation functions are array methods that directly modify the original array they are called upon. When
you use mutation functions, you alter the content of the array without creating a new array. These
functions change the array's length or its elements, or both, and typically return nothing (undefined).
1) push ():
methodPush() {
let shoppingCart = ['apple', 'banana', 'orange'];
let newItem = 'grapes';
shoppingCart.push(newItem);
console.log("Updated Shopping Cart:", shoppingCart);
}
In this scenario, push () is used because we want to add an element to the end of an existing array
(shoppingCart).
We start by defining an array shoppingCart containing some initial items. We specify a newItem
that we want to add to the shoppingCart array.
We use the push() method to add the newItem to the end of the shoppingCart array.
The push() method modifies the original array by adding the specified element to the end of it.
Note: The push() method modifies the original array and returns the new length of the array after
the element has been added.
2) pop ():
methodPop() {
let stackOfBooks = ['Book 1', 'Book 2', 'Book 3', 'Book 4'];
let removedBook = stackOfBooks.pop();
console.log("Removed Book:", removedBook);
console.log("Updated Stack of Books:", stackOfBooks);
}
In this scenario, pop() is used because we want to remove the last element from an existing array
(stackOfBooks), simulating the behavior of a stack where elements are added and removed from
the top.
We start by defining an array stackOfBooks containing some initial books. We use the pop ()
method to remove the last element (top book) from the stackOfBooks array.
The pop () method modifies the original array by removing the last element and returns the
removed element. We capture the removed book in a variable removedBook so that we can
access it if needed.
1
Advance Angular v.17
2. Array Functions - 2
Note: The pop () method modifies the original array and returns the removed element. If the array
is empty, pop () returns undefined.
EXTRACTION FUNCTIONS
Extraction functions are array methods that do not modify the original array. Instead, they create a new
array or return a single value based on the elements of the original array. These functions leave the
original array unchanged and typically return a new array or some value derived from the original array.
1) slice ():
Syntax:------------>>>.array.slice(begin, end)
array: The array from which you want to extract elements.
begin: The index at which to begin extraction. It's optional. If omitted, the slice begins from index
0. If negative, it indicates an offset from the end of the array (e.g., -1 means the last element).
end: The index before which to end extraction. The extracted slice will include elements up to,
but not including, this index. It's also optional. If omitted, the slice extracts through to the end of
the array. If negative, it indicates an offset from the end of the array.
methodSlice() {
let tasks = ['Task 1', 'Task 2', 'Task 3', 'Task 4', 'Task 5'];
1: This is the start index for extraction. It indicates that we want to start extracting elements from
index 1.
Index 1 corresponds to the second task in the tasks array, which is 'Task 2'.
4: This is the end index for extraction. It indicates that we want to stop extracting elements before
index 4.
Index 4 corresponds to the fifth task in the tasks array, which is 'Task 5'.
2
Advance Angular v.17
2. Array Functions - 2
The slice() method extracts a shallow copy of the elements from the tasks array starting from the
start index (inclusive) up to, but not including, the end index (exclusive). Therefore, it extracts
elements from index 1 up to (but not including) index 4.
Result (selectedTasks):
The slice() method returns a new array containing the extracted elements.
The extracted elements are 'Task 2', 'Task 3', and 'Task 4'.
These tasks are stored in the variable selectedTasks.
Therefore, selectedTasks now holds ['Task 2', 'Task 3', 'Task 4'].
2) flat ():
methodFlat() {
let nestedArray = [[1, 2], [3, 4], [5, 6]];
In this scenario, flat () is used because we want to flatten a nested array structure into a single-
level array.
a) We start by defining an array nestedArray containing nested arrays, each representing a different
category of items.
b) We use the flat () method to flatten the nestedArray. The flat () method creates a new array with
all sub-array elements concatenated into it recursively up to the specified depth.
c) In this example, since we don't specify any depth, flat () will flatten the array to one level by
default.
The flat () method returns a new flattened array, and we store it in the variable flattenedArray.
Note: If the nested arrays have varying depths, you can pass a depth parameter to flat () to specify
how many levels of nested arrays should be flattened. If the depth is not specified, it defaults to 1.
If you pass Infinity as the depth, it will flatten all nested arrays to any depth.
3) flatMap ():
methodFlatMap() {
let sentences = ["Hello world", "How are you", "I am fine"];
let words = sentences.flatMap(sentence => {
return sentence.split(" ");
});
console.log("Words:", words);
}
3
Advance Angular v.17
2. Array Functions - 2
In this scenario, flatMap() is used because we want to transform each element of an array
(sentences), split them into multiple elements (words), and then flatten the resulting array of
arrays into a single-level array.
a) We use the flatMap() method to iterate over each sentence in the sentences array. For each
sentence, we split it into words using the split () method, which returns an array of words.
b) Inside the flatMap() function, we define an arrow function that takes each sentence as an
argument. Within this arrow function:
We split the sentence into words using the split(" ") method.
We return the array of words.
c) The flatMap() method then flattens the array of arrays into a single-level array by
concatenating all the sub-array elements.
Note: The flatMap() method is similar to map(), but it also flattens the resulting array by one
level. If you don't need to flatten the array, you can use map () followed by flat (), but flatMap()
combines both operations into one.