Method Flashcards
Define .slice( ) method.
The slice( ) method returns a shallow copy of a portion of an array into a new array object selected from start to end. (start and end represent the index of items in that array. end is not included)
ex.
const numbers=[1,2,3,4,5]
numbers.slice(1,3) = [2,3]
numbers.slice()=[1,2,3,4,5] => returns shallow copy
Define .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.
ex.
‘abc’.indexOf(‘b’) = 1
‘abc’.indexOf(‘x’) =-1
.push( )
the push( ) method adds one or more elements to the end of an array and returns the new length of the array
ex.
const animals =[‘pigs’, ‘goats’, ‘sheep’]
const count = animals.push(‘cows’)
console.log(count); 4
console.log(animals); [[‘pigs’, ‘goats’, ‘sheep’, ‘cows’]
.pop( )
The pop( ) method removes the last element from an array and returns that element. ex. const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; console.log(plants.pop()); // expected output: "tomato"
console.log(plants); // expected output: Array [“broccoli”, “cauliflower”, “cabbage”, “kale”]
.shift( )
The shift( ) method removes the first element from an array and returns that removed element.
arr.unshift( )
The unshift( ) method adds one ore more elements to the beginning of an array and returns the new length of the array
ex. arr. unshift(element1[,…[,elementN]])
const array = [1,2,3]
array.unshift(4,5)
console.log(array)// [1,2,3,4,5]
arr. join(‘-‘) // ‘x-b-c’
arr. join(‘ ‘) // ‘xbc’
arr. join( ) // ‘x,b,c’
all elements in a single string
.map( )
It takes an argument of a callback funtion and returns a new array.
it works in a similar manner to .forEach()–the major difference is that .map returns a new array.
.forEach( )
iteration method.
it executes the same code for each element of an array.
loops through the array and executes the callback function for each element. During each execution, the current element is passed as an argument to the callback function.
the return value for .forEach() will always be undefined.
.filter( )
Returns a new array. However, it returns an array of elements after filtering out certain elements from original array. callback function for the .filter( ) method should return true or false.
The elements that causes the callback function to return true are added to the new array.
callback function
a function passed as an argument into another function
.findIndex( )
it returns the index of the first element that evaluates to true in the callback function. it returns -1 if none of the elements in the array satisfies the condition
? true : false;
return true or else return false
? -> ternary operator
: -> colon separator
.reduce( )
It returns a single value after iterating through the elements of an array, thereby reducing the array.
value of acculumator= starts off as the value of the first element in the array. The return value of the callback function becomes the accumulator
value of currentValue = starts as the second element.it takes on the value of the current element in the looping process.
.some( )
tests whether at least one element in the array passes the test implemented by the provided function