Arrays : Methods and Properties Flashcards
array.shift()
Removes the first element from an array and returns that element. This method changes the length of the array.
array.sort()
The sort method sorts the elements of an array in place and returns the array. If compareFunction parameter isn’t supplied, elements are sorted by being converted to strings and comparing strings in Unicode code point value.
array.unshift(element1, …elementn);
Adds one or more elements to the front of the array. Takes one or more arguments. Returns the new length of the array.
str = array.join([separator = ‘ ‘ ])
Joins the elements in an array into a string. Separator is optional and specifies a string to separate each element. If not present, the array elements are separated by a comma within the string.
array.length
This property returns the number of elements within an array.
array.push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
Syntax: arr.push(element1, …, elementN)
array.slice()
The slice() method returns a shallow copy of a portion of an array into a new array object.
Syntax: arr.slice([begin[, end]])
Parameters use index values of arrays
array.splice()
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
Syntax
array.splice(start, deleteCount[, item1[, item2[, …]]])
Parameters:
start
Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array.
deleteCount
An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.
itemN
The element to add to the array. If you don’t specify any elements, splice() will only remove elements from the array.
array.indexOf()
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
Syntax
arr.indexOf(searchElement[, fromIndex = 0])
Parameters:
searchElement
Element to locate in the array.
fromIndex
The index to start the search at. If the index is greater than or equal to the array’s length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (entire array is searched).
array.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
Syntax: arr.filter(callback[, thisArg])
Parameters:
callback
Function to test each element of the array. Invoked with arguments (element, index, array). Return true to keep the element, false otherwise.
thisArg
Optional. Value to use as this when executing callback.
array.splice()
The splice() method changes the content of an array by removing existing elements and/or adding new elements..
Syntax: arr.splice(start, deleteCount, [item1, item2…itemN)
start
Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end.
deleteCount
An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.
itemN
The element to add to the array. If you don’t specify any elements, splice() will only remove elements from the array.
array.map()
The map() method creates a new array with the results of calling a provided function on every element in this array.
Syntax: arr.map(callback[, thisArg])
Parameters:
callback
Function that produces an element of the new Array, taking three arguments:
- currentValue
The current element being processed in the array. - index
The index of the current element being processed in the array. - array
The array map was called upon.
thisArg
Optional. Value to use as this when executing callback.