Please enable JavaScript.
Coggle requires JavaScript to display documents.
Arrays (Combine,slice and Joining arrays (Combining Arrays (We use the…
Arrays
Combine,slice and Joining arrays
Combining Arrays
- We use the concat() to join 2 arrays: array1.concat(array2);
- Use the spread operator to combine one or more arrays.
const combinedArray= [...array1, ...array2];
Slicing Arrays
- We use the slice() method to work with a given array or parts of the array
- const sliceArray = myArray.slice(2);
With only one parameter, it specifies the starting point.
- const sliceArray = myArray.slice();
With no parameters, it makes a copy of the array.
Important
- If using primitive values, the values will be copied
- If using objects, it is the reference to the object that will be copied
Joining Arrays
- Use the join() method to join array items (Using a seperator or a space as a seperator).
const joinedArray = myArray.join(',');
-
-
IMPORTANT METHODS
- The filter() method:
- Loop through every element in the array and will return true or false.
- If the condition returns true, then, the array item is copied to the new array, if false, then, the item is not copied.
- The map() method:
- Loop through every element in the original array, perform some action and then generate a new Array based on our actions.
- The map method has very similar syntax to the forEach method.
- The primary difference is that the map function needs a return statement in order to create the new Array
const myArray = [1,2,3,4,5];
const result = myArray.map(function(item, index) {
// Do something here.
// Return tatement.
});
- The reduce() method reduces the array to a single value.
- The reduce() method takes 2 parameters, a callback function and an initial value, in the callback function, the first parameter is the value been passed in and the second is the value in the array.
- The reduce() method executes a provided function for each value of the array (from left-to-right).
- The return value of the function is stored in an accumulator (result/total).
-
Adding Elements
- Add to the end of an array: myArray.push('myItem').
- Add to the start of the array: myArray.unshift(1,2)
- Add anywhere of the array: myArray.splice(2,0,'firstItem', 'secondItem')
- Note: The first argument of the splice method is the starting index for the new values to be inserted, the second argument is the number of values to delete and from the third argument, the new values to be inserted.
Finding Primitive
- myArray.indexOf(3, 'char'): Return last index number of the item in the array
- myArray.lastIndexOf(1, 3): Return the last index number of the item in the array
- myArray.includes(1): Return true if the value specified is found in the array
- Note: If two arguments are specified, the first represents the starting index and the second the item to look for.
Finding Objects
- Using the find() method to find the item :
const result = myArray.find(function(something){
return something.property === 'someValue';
});
console.log(result).
The code above will return the objecif if found or false if not
- Using the findIndex() method to find the item index :
const result = myArray.findIndex(function(something){
return something.property === 'someValue';
});
console.log(result).
The code above will return the index if found or -1 if not
Removing Elements:
- Remove the last element from an array: myArray.pop()
- Remove the first element from an array: myArray.shift()
- Remove an element from any place in the array: myArray.splice(2, 1)
Emptying an array
- Reassign the existing array to an empty one (Works if there are no other references to the original array).
- Truncate the array using the lenth() method: myArray.length = 0, this will remove all array items 'Also in all other references).
- Using the splice() method: myArray.splice(0, myArray.length)
Iterating through arrays
- We use the for...of and the forEach() loop
- Note that the forEach loop can take 2 parameters, one for the item and one for index.