Skip to content

Instantly share code, notes, and snippets.

@eugenserbanescu
Last active July 30, 2022 10:27
Show Gist options
  • Save eugenserbanescu/60bfbef8ea7847a92cc6522659bf97f0 to your computer and use it in GitHub Desktop.
Save eugenserbanescu/60bfbef8ea7847a92cc6522659bf97f0 to your computer and use it in GitHub Desktop.
Javascript array functions
let list = [1,2,3,4];
let forEachCalled = 0;
list.forEach(item => {
// this one does something for every item in the array
// and returns the original array
forEachCalled++;
});
const mapped = list.map(item => {
// this one runs this function for every element of the array. It returns a new array where every value is whatever you return in this function
return 'a' + item;
}); // mapped will be equal to ['a1','a2','a3','a4']
const filtered = list.filter(item => {
// this one will loop through the entire array and return a new array with only the elements for which you return true from here
// as in ... elements that pass your conditional
return item > 2;
}); // should return [3,4];
const foundItem = list.find(item => {
// similar to filter, only this one stops iterating as soon as it found one element that fist your criteria
// also returns the said element
return item > 2;
}); // should return 3
// note: array.some does more or less the same thing, but returns true/false
const oneElementLessThan2 = list.some(item => item > 2) // should return true
const sum = list.reduce((accumulator, element) => {
// this one will execute the function you define here for every element in the array, passing on whatever you return as the accumulator.
// the 2nd argument that reduce takes is the initial accumulator value
return accumulator + element
}, 0)// this will return 10
// you can use reduce to transform data types (in this case an array of numbers transforms into an object)
const objectFromList = list.reduce((accumulator, element) => {
accumulator[`element${element}`] = element // we're setting the key element1, element2, etc on our accumulator object for the current element
return accumulator// and we return it so the next iteration can add its own key and value
}, {})// the initial value of accumulator is an empty object
// this will return {element1: 1, element2: 2, element3: 3, element4: 4}
list.includes(4); // should return true. Similar to find, this checks if a value exists in an array and returns true or false
list.indexOf(4); // should return 3. Similar to find, this one gives you the index at which an element is located in an array
//this will return the elements starting the index you pass all the way to the end of the array (including the one you pass)
list.slice(2) // [3, 4]
// this will return elements between indexes 1 and 3 (including index 1, 2 and 3, but excluding 4)
list.slice(1, 4) // [2, 3, 4]
// From here on out those methods will alter the array
list.push(5); // this one will add the element 5 to the end of the array
list.splice(0,1) // this one removes elements from the array.
// The first argument is the index at which to start, and the second one is the number of elements to remove
// splice returns the removed elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment