-
-
Save minmaxdata/e44dbcf9dc9b9d6da3a356a1991de67c to your computer and use it in GitHub Desktop.
Example of array methods: forEach, map, filter, some, every, indexOf, and include. (ES2015 syntax)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* forEach | |
*/ | |
// forEach is very simlar to for-looping arr.length | |
// array.forEach(callback, context) | |
var arr = ['apple', 'orange', 'watermelon', 10, 20, 30] | |
arr.forEach((value, index) => console.log(`Element's ${index} type is ${typeof value}`)) | |
// Prints: | |
// Element 0 type is string | |
// Element 1 type is string | |
// Element 2 type is string | |
// Element 3 type is number | |
// Element 4 type is number | |
// Element 5 type is number | |
/* | |
* map | |
*/ | |
// array.map(callback, context) | |
// returns an array | |
// will return a new array of elements from 'array' after applying the callback function on each element | |
// alyways returns same size of array | |
var arr2 = [ | |
{ fruit:'apple', color:'Red' }, | |
{ fruit:'orange', color:'Blue' }, | |
{ fruit:'watermelon', color:'Green' } | |
] | |
var arr2map = arr2.map(obj => obj.color) | |
console.log(arr2map) | |
// Prints: | |
// [ 'Red', 'Blue', 'Green' ] | |
/* | |
* filter | |
*/ | |
// arr.filter(callback, context) | |
// will return a new array with item that only satisfy a specific condition we provide in the callback function | |
var arr = ['apple', 'orange', 'watermelon', 10, 20, 30] | |
var numbersOnly = arr.filter(num => typeof num === 'number') | |
console.log(numbersOnly) | |
// Prints: | |
// [ 10, 20, 30 ] | |
/* | |
* some | |
*/ | |
// arr.some(callback, context) | |
// returns boolean | |
// will apply a function on each element in the array and return true once it finds one, or false if the list has been exhausted | |
var arr3 = [10, 20, 30] | |
var checkFor = num => arr3.some( element => element === num ) | |
console.log(checkFor(20)); // true | |
console.log(checkFor(40)); // false | |
/* | |
* every | |
*/ | |
// arr.every(callback, context) | |
// returns a boolean | |
// will stop and return false once it finds an elements that doesn't match the condition in the callback function | |
var arr4 = [10, 20, 30] | |
var arr4every = num => arr4.every( element => element > num ) | |
console.log(arr4every(9)); // true | |
console.log(arr4every(15)); // false | |
console.log(arr4every(40)); // false | |
/* | |
* indexOf | |
*/ | |
// arr.indexOf(value, fromIndex) | |
// returns a 'number' of which that value is at from the beggining of the array, or (optionally) from 'fromIndex' | |
// or returns '-1' if not exist | |
var arr5 = ['Green', 'Red', 'Blue', 'G'] | |
console.log(arr5.indexOf('blue')) // -1 | |
console.log(arr5.indexOf('Blue')) // 2 | |
console.log(arr5.indexOf('Green')) // 0 | |
console.log(arr5[2]) // Blue | |
/* | |
* includes | |
*/ | |
const a = [1, 2, 3]; | |
a.includes(2); // true | |
a.includes(4); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment