Tuesday, February 24, 2015

Mutable or Immutable that is the question



The JavaScript Array Object provides a lot of methods to work with.

For me the difficult is to remember which is Mutable and which is Immutable.

Shortly, Immutable are methods (or the entire object) that doesn't change the state of the object; The string object is an example of this behavior.
On the other hand Mutable methods change the state of the object.

Below there is a table with array's methods and the column "Im\Mutable" suggest when a method is Mutable or Immutable.
Pay attention at slice and splice methods that sound similar but respectively are Immutable and Mutable.





















Method Im\Mutable Description Note Sample
concat Immutable As name imply returns a new array with concatenation of the arrays var newArray = array1.concat(array2);
indexOf Immutable It look for the first occurrence of known element and return its index It returns -1 if not found. Use identity check. Begin index start at zero. var index = myArray.indexOf('target',beginIndex);
lastIndexOf Immutable It look for the last occurrence of known element and return its index It returns -1 if not found. Use identity check. Begin index start at zero. var index = myArray.lastIndexOf('target',beginIndex);
join Immutable It returns a single string containing all the array elements separated by a specified string separator var newString = myArray.join(',');
reverse Mutable Reverse the order of all elements in the array Returns void myArray.reverse();
sort Mutable Sort all elements in the array in ascending order Returns void myArray.sort();
slice Immutable Extract one or more elements from the array and copy then to result array Parameters are: start index, end index. Ending index isn't included in extraction var newArray = myArray.slice(startIndex,endIndex);
splice Mutable It returns a new array with only the replaced element of the source array. It replace elements in the original array. Parameters: Index for the first element to be replaced. Number of the elements to replace. Values to use in replacement operation. If values are omitted the element are removed instead replaced. var newArray = myArray.splice(1,3,'newValue1','newValue2','newValue3');
pop Mutable Removes the last element of the array and returns it. var lastElement = myArray.pop();
push Mutable Adds an element at the end of the array and returns the new length var newLength = myArray.push('newElement')
shift Mutable Removes the first element of the array and return it. If array is empty the return value will be undefined. var firstElement = myArray.shift();
unshift Mutable Adds an element at the beginning of the array and returns the new length var newLength = myArray.unshift('newElement');
every Immutable Checks if all elements in the array respect a condition provided as function argument Last parameter is optional (is a reference to the array object) var everyMeetRequirement = myArray.every(myFunction,myArray);

function myFunction(value,index,array){
}

some Immutable Checks if at least one element in the array respect a condition provided as function argument. If all element doesn't meet requirement it return false. Last parameter is optional (is a reference to the array object) var someMeetRequirement = myArray.some(myFunction,myArray);
function myFunction(value,index,array){
}
foreach Immutable Provided function argument is called for every element in the array in ascending index order. myArray.forEach(myFunction);

function myFunction(value,index,array){
}
filter Immutable Returns a new array with elements of the array that meets a logical condition provided by a function argument var newArray = myArray.filter(myFunction,myArray);
function myFunction(value,index,array){
}
map Immutable Enable to evaluate all the elements in the array and return a substitute the element value, all those elements are returned in a new array. var newArray = myArray.map(myFunction);
function myFunction(value,index,array){
}
reduce Immutable Is a recursive method. Each result of function argument is passed as first argument to the function (as previous result). This method call the argument function in ascending index order. var newValue = myArray.reduce(myFunction);
function myFunction(previousReturnValue, currentElementValue) {
}
reduceRight Immutable Is a recursive method. Each result of function argument is passed as first argument to the function (as previous result). This method call the argument function in descending index order. var newValue = myArray.reduce(myFunction);
function myFunction(previousReturnValue, currentElementValue) {
}


No comments:

Post a Comment