-
-
Save BouncingBison/ed53b99ee0e2ce9db96242c712d2015a to your computer and use it in GitHub Desktop.
Revisions
-
ourmaninamsterdam revised this gist
Aug 20, 2015 . 1 changed file with 18 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -26,12 +26,12 @@ This is a work-in-progress cheatsheet for JS arrays. Please feel free to leave a * [Execute a function once per array item](#user-content-execute-a-function-once-per-array-item) * [Filter an array](#user-content-filter-an-array) * [Detect an array](#user-content-detect-an-array) * [ES5 and above](#user-content-es5-and-above) * [ES4 and below](#user-content-es4-and-below) * [Simple FIFO queue](#user-content-simple-fifo-queue) * [Find index of an item](#user-content-find-index-of-an-item) * [ES5 and above](#user-content-es5-and-above-1) * [ES4 and below](#user-content-es4-and-below-1) * [Randomise an array](#user-content-randomise-an-array) * [Chaining Methods](#chaining-methods) @@ -300,25 +300,25 @@ meals.filter(function(item) { ``` ## Detect an array ### ES5 and above ```javascript var meals = ['breakfast', 'lunch', 'dinner']; Array.isArray(meals) // true ``` ### ES4 and below ```javascript var meals = ['breakfast', 'lunch', 'dinner']; function isArray(arr) { return !!(Object.prototype.toString.call(arr) === '[object Array]'); } isArray(meals); // true ``` @@ -341,6 +341,14 @@ meals.push('afternoon tea'); ## Find index of an item ## ES5 and above ```javascript var meals = ['breakfast', 'elevenses', 'brunch']; meals.indexOf('brunch'); // 2 ``` ### ES4 and below ```javascript @@ -366,14 +374,6 @@ inArray(meals, 'dinner'); // -1 ``` ## Randomise an array ```javascript -
ourmaninamsterdam revised this gist
Aug 20, 2015 . 1 changed file with 15 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -43,6 +43,15 @@ var meals = ['breakfast', 'lunch', 'dinner'] ; ## Empty an array Keeping references intact. ```javascript var meals = ['breakfast', 'lunch', 'dinner']; meals.splice(0, meals.length); ``` or ```javascript var meals = ['breakfast', 'lunch', 'dinner']; meals.length = 0 @@ -168,7 +177,7 @@ meals; var meals = ['breakfast', 'lunch', 'dinner']; meals.splice(1, 2); // ['lunch', 'dinner'] meals; // ['breakfast'] @@ -338,16 +347,15 @@ meals.push('afternoon tea'); var meals = ['breakfast', 'elevenses', 'brunch']; function inArray(arr, item){ var found = -1, i = arr.length; while(--i >= 0) { if(arr[i] === item){ found = i; break; } } return found; } -
ourmaninamsterdam revised this gist
Aug 16, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -71,7 +71,7 @@ Or ```javascript var meals = ['breakfast', 'lunch', 'dinner']; meals.slice(-1)[0]; // 'dinner' ``` @@ -284,7 +284,7 @@ meals.forEach(function(currentValue, index, arr){ ```javascript var meals = ['breakfast', 'lunch', 'dinner', 'supper']; meals.filter(function(item) { return item !== 'breakfast'; }); // ['lunch', 'dinner', 'supper']; -
ourmaninamsterdam revised this gist
Aug 14, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ var meals = ['breakfast', 'lunch', 'dinner'] ; ## Empty an array ```javascript var meals = ['breakfast', 'lunch', 'dinner']; meals.length = 0 ``` -
ourmaninamsterdam revised this gist
Aug 8, 2015 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -53,7 +53,7 @@ meals.length = 0 ```javascript var meals = ['breakfast', 'lunch', 'dinner']; var copy = meals.slice(); // ['breakfast', 'lunch', 'dinner'] ``` @@ -337,16 +337,17 @@ meals.push('afternoon tea'); ```javascript var meals = ['breakfast', 'elevenses', 'brunch']; function inArray(arr, item){ var found = -1, len = arr.length, i = len; while(--i) { if(arr[i] === query){ found = i; } } return found; } -
ourmaninamsterdam revised this gist
Aug 8, 2015 . 1 changed file with 25 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,6 @@ This is a work-in-progress cheatsheet for JS arrays. Please feel free to leave a * [Remove single item at a specific index](#user-content-remove-single-item-at-a-specific-index) * [Remove several items](#user-content-remove-several-items) * [Reverse an array](#user-content-reverse-an-array) * [Delimit an array](#user-content-delimit-an-array) * [Sort in numerical order](#user-content-sort-in-numerical-order) * [Sort in alphabetical order](#user-content-sort-in-alphabetical-order) @@ -33,6 +32,7 @@ This is a work-in-progress cheatsheet for JS arrays. Please feel free to leave a * [Find index of an item](#user-content-find-index-of-an-item) * [ES4 and below](#user-content-es4-and-below-1) * [ES5 and above](#user-content-es5-and-above-1) * [Randomise an array](#user-content-randomise-an-array) * [Chaining Methods](#chaining-methods) ## Create an array @@ -183,30 +183,6 @@ meals.reverse(); // ['dinner', 'lunch', 'breakfast']; ``` ## Delimit an array ```javascript @@ -389,6 +365,30 @@ meals.indexOf('brunch'); // 2 ``` ## Randomise an array ```javascript function randomiseArray(arr) { var buffer = [], start; for(var i = arr.length; i >= arr.length && i > 0;i--) { start = Math.floor(Math.random() * arr.length); buffer.push(arr.splice(start, 1)[0]) }; return buffer; } randomiseArray([0,1,2,3,4]); // [2,1,0,3,4] randomiseArray([0,1,2,3,4]); // [3,2,1,4,0] randomiseArray([0,1,2,3,4]); // [1,2,4,0,3] ``` # Chaining methods ```javascript -
ourmaninamsterdam revised this gist
Aug 8, 2015 . 1 changed file with 0 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -41,12 +41,6 @@ This is a work-in-progress cheatsheet for JS arrays. Please feel free to leave a var meals = ['breakfast', 'lunch', 'dinner'] ; ``` ## Empty an array ```javascript -
ourmaninamsterdam revised this gist
Aug 8, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,8 @@ This is a work-in-progress cheatsheet for JS arrays. Please feel free to leave a * [Reverse an array](#user-content-reverse-an-array) * [Randomise an array](#user-content-randomise-an-array) * [Delimit an array](#user-content-delimit-an-array) * [Sort in numerical order](#user-content-sort-in-numerical-order) * [Sort in alphabetical order](#user-content-sort-in-alphabetical-order) * [Join two arrays together](#user-content-join-two-arrays-together) * [Copy specific item(s)](#user-content-copy-specific-items) * [Augment items within an array](#user-content-augment-items-within-an-array) -
ourmaninamsterdam revised this gist
Aug 8, 2015 . 1 changed file with 9 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -223,29 +223,25 @@ meals.join(' AND '); // 'breakfast AND lunch AND dinner' ``` ## Sort in alphabetical order ```javascript var meals = ['dinner', 'supper', 'breakfast', 'lunch']; meals.sort(); // ['breakfast', 'dinner', 'lunch', 'supper'] ``` ## Sort in numerical order ```javascript var numbers = [1438,2605,794,3947,6241,11745,2585]; numbers.sort(function(a, b) { return a - b; }); // [794,1438,2585,2605,3947,6241,11745] ``` ## Join two arrays together -
ourmaninamsterdam revised this gist
Aug 8, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -87,7 +87,7 @@ meals.slice(-1); var meals = ['breakfast', 'lunch', 'dinner']; meals.shift(); // 'breakfast' meals; -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Arrayzing - The JavaScript array cheatsheet This is a work-in-progress cheatsheet for JS arrays. Please feel free to leave a comment if this has helped you or you would like to suggest anything. * [Create an array](#user-content-create-an-array) * [Empty an array](#user-content-empty-an-array) -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 2 changed files with 23 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ The MIT License (MIT) Copyright (c) 2015 Justin Perry Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ # Arrayzing - The JavaScript array cheatsheet This is a WIP cheatsheet to help those on get a little stuck when. * [Create an array](#user-content-create-an-array) * [Empty an array](#user-content-empty-an-array) * [Clone an array](#user-content-clone-an-array) @@ -451,4 +453,4 @@ getMealsByMaxCalories(meals, 850, 2000); } ] */ ``` -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # Arrayzing - The JavaScript array cheatsheet * [Create an array](#user-content-create-an-array) * [Empty an array](#user-content-empty-an-array) -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 8 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -191,28 +191,24 @@ meals.reverse(); ```javascript function randomiseArray(arr) { var buffer = [], start; for(var i = arr.length; i >= arr.length && i > 0;i--) { start = Math.floor(Math.random() * arr.length); buffer.push(arr.splice(start, 1)[0]) }; return buffer; } randomiseArray([0,1,2,3,4]); // [2,1,0,3,4] randomiseArray([0,1,2,3,4]); // [3,2,1,4,0] randomiseArray([0,1,2,3,4]); // [1,2,4,0,3] ``` ## Delimit an array -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -58,8 +58,6 @@ meals.length = 0 var meals = ['breakfast', 'lunch', 'dinner']; var copy = meals.slice(0, meals.length); // ['breakfast', 'lunch', 'dinner'] ``` -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -52,6 +52,17 @@ var meals = new Array('breakfast', 'lunch', 'dinner'); meals.length = 0 ``` ## Clone an array ```javascript var meals = ['breakfast', 'lunch', 'dinner']; var copy = meals.slice(0, meals.length); copy; // ['breakfast', 'lunch', 'dinner'] ``` ## Get last item ```javascript -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,9 @@ # Array Cheatsheet * [Create an array](#user-content-create-an-array) * [Empty an array](#user-content-empty-an-array) * [Clone an array](#user-content-clone-an-array) * [Get last item](#user-content-get-last-item) * [Remove first item](#user-content-remove-first-item) * [Remove last item](#user-content-remove-last-item) * [Add new item(s) to beginning](#user-content-add-new-items-to-beginning) @@ -32,7 +33,7 @@ * [ES5 and above](#user-content-es5-and-above-1) * [Chaining Methods](#chaining-methods) ## Create an array ```javascript var meals = ['breakfast', 'lunch', 'dinner'] ; @@ -51,7 +52,7 @@ var meals = new Array('breakfast', 'lunch', 'dinner'); meals.length = 0 ``` ## Get last item ```javascript -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -197,8 +197,10 @@ function randomiseArray(arr) { randomiseArray(arr); // [2,1,0,3,4] randomiseArray(arr); // [3,2,1,4,0] randomiseArray(arr); // [1,2,4,0,2] ``` -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 17 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -181,13 +181,26 @@ meals.reverse(); ```javascript function randomiseArray(arr) { if(Object.prototype.toString.call(arr) !== '[object Array]') return; var copy = arr.slice(0, arr.length), buffer = [], start; for(var i = copy.length; i >= copy.length && i > 0;i--) { start = parseInt(Math.random() * copy.length, 16); buffer.push(copy.splice(start, 1)[0]) }; return buffer; } randomiseArray(arr); // [2,1,0,3,4] randomiseArray(arr); // [3,2,1,4,0] randomiseArray(arr); // [1,2,4,0,2] ``` ## Delimit an array -
ourmaninamsterdam revised this gist
Aug 6, 2015 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,7 @@ * [Remove single item at a specific index](#user-content-remove-single-item-at-a-specific-index) * [Remove several items](#user-content-remove-several-items) * [Reverse an array](#user-content-reverse-an-array) * [Randomise an array](#user-content-randomise-an-array) * [Delimit an array](#user-content-delimit-an-array) * [Sort in alphabetical/numerical order](#user-content-sort-in-alphabeticalnumerical-order) * [Sort in reverse alphabetical/numerical order](#user-content-sort-in-reverse-alphabeticalnumerical-order) @@ -176,6 +177,19 @@ meals.reverse(); // ['dinner', 'lunch', 'breakfast']; ``` ## Randomise an array ```javascript function randomiseArray(arr) { var buffer = [], start; for(var i = arr.length; i >= arr.length && i > 0;i--) { start = parseInt(Math.random() * arr.length, 16); buffer.push(arr.splice(start,1)[0]) }; return buffer; } ``` ## Delimit an array ```javascript -
ourmaninamsterdam revised this gist
Aug 4, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -296,6 +296,8 @@ var meals = ['breakfast', 'lunch', 'dinner']; function isArray(arr) { return !!(Object.prototype.toString.call(arr) === '[object Array]'); } isArray(meals); // true ``` -
ourmaninamsterdam revised this gist
Aug 3, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,8 +5,8 @@ * [Get last item](#user-content-getting-last-item) * [Remove first item](#user-content-remove-first-item) * [Remove last item](#user-content-remove-last-item) * [Add new item(s) to beginning](#user-content-add-new-items-to-beginning) * [Add new item(s) to end](#user-content-add-new-items-to-end) * [Overwrite item at a specific index](#user-content-overwrite-item-at-a-specific-index) * [Add new item(s) at a specific index](#user-content-add-new-items-at-a-specific-index) * [Remove single item at a specific index](#user-content-remove-single-item-at-a-specific-index) -
ourmaninamsterdam revised this gist
Aug 3, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,8 +5,8 @@ * [Get last item](#user-content-getting-last-item) * [Remove first item](#user-content-remove-first-item) * [Remove last item](#user-content-remove-last-item) * [Add new item(s) to beginning](#user-content-add-new-item-to-beginning) * [Add new item(s) to end](#user-content-add-new-item-to-end) * [Overwrite item at a specific index](#user-content-overwrite-item-at-a-specific-index) * [Add new item(s) at a specific index](#user-content-add-new-items-at-a-specific-index) * [Remove single item at a specific index](#user-content-remove-single-item-at-a-specific-index) -
ourmaninamsterdam revised this gist
Aug 3, 2015 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,10 +23,12 @@ * [Execute a function once per array item](#user-content-execute-a-function-once-per-array-item) * [Filter an array](#user-content-filter-an-array) * [Detect an array](#user-content-detect-an-array) * [ES4 and below](#user-content-es4-and-below) * [ES5 and above](#user-content-es5-and-above) * [Simple FIFO queue](#user-content-simple-fifo-queue) * [Find index of an item](#user-content-find-index-of-an-item) * [ES4 and below](#user-content-es4-and-below-1) * [ES5 and above](#user-content-es5-and-above-1) * [Chaining Methods](#chaining-methods) ## Creating an array -
ourmaninamsterdam revised this gist
Aug 3, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ * [Return true if at least one item matches a condition](#user-content-return-true-if-at-least-one-item-matches-a-condition) * [Execute a function once per array item](#user-content-execute-a-function-once-per-array-item) * [Filter an array](#user-content-filter-an-array) * [Detect an array](#user-content-detect-an-array) * [Simple FIFO queue](#user-content-simple-fifo-queue) * [Find index of an item](#user-content-find-index-of-an-item) * [ES4 and below](#user-content-es4-and-below) -
ourmaninamsterdam revised this gist
Aug 3, 2015 . 1 changed file with 25 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -22,6 +22,7 @@ * [Return true if at least one item matches a condition](#user-content-return-true-if-at-least-one-item-matches-a-condition) * [Execute a function once per array item](#user-content-execute-a-function-once-per-array-item) * [Filter an array](#user-content-filter-an-array) * [Detect an array[(#user-content-detect-an-array) * [Simple FIFO queue](#user-content-simple-fifo-queue) * [Find index of an item](#user-content-find-index-of-an-item) * [ES4 and below](#user-content-es4-and-below) @@ -90,7 +91,7 @@ meals; // ['breakfast', 'lunch']; ``` ## Add new item(s) to beginning ```javascript var meals = ['lunch', 'dinner']; @@ -102,7 +103,7 @@ meals; // ['breakfast', 'lunch', 'dinner'] ``` ## Add new item(s) to end ```javascript @@ -267,6 +268,7 @@ meals.some(function(item){ return item === 'burgers!!';}); ```javascript var meals = ['breakfast', 'lunch', 'dinner', 'supper']; meals.forEach(function(currentValue, index, arr){ console.log(index, currentValue, arr); }); @@ -282,6 +284,27 @@ meals.filter(function() (item) { }); // ['lunch', 'dinner', 'supper']; ``` ## Detect an array ### ES4 and below ```javascript var meals = ['breakfast', 'lunch', 'dinner']; function isArray(arr) { return !!(Object.prototype.toString.call(arr) === '[object Array]'); } // true ``` ### ES5 and above ```javascript var meals = ['breakfast', 'lunch', 'dinner']; Array.isArray(meals) // true ``` ## Simple FIFO queue -
ourmaninamsterdam revised this gist
Aug 1, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -60,6 +60,7 @@ meals[meals.length - 1]; Or ```javascript var meals = ['breakfast', 'lunch', 'dinner']; meals.slice(-1); // 'dinner' ``` -
ourmaninamsterdam revised this gist
Aug 1, 2015 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,11 @@ var meals = ['breakfast', 'lunch', 'dinner']; meals[meals.length - 1]; // 'dinner' ``` Or ```javascript meals.slice(-1); // 'dinner' ``` -
ourmaninamsterdam revised this gist
Aug 1, 2015 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ # Array Cheatsheet * [Create an array](#user-content-creating-an-array) * [Empty an array](#user-content-empty-an-array) * [Get last item](#user-content-getting-last-item) * [Remove first item](#user-content-remove-first-item) * [Remove last item](#user-content-remove-last-item) @@ -39,6 +40,13 @@ Or var meals = new Array('breakfast', 'lunch', 'dinner'); ``` ## Empty an array ```javascript var meals = new Array('breakfast', 'lunch', 'dinner'); meals.length = 0 ``` ## Getting last item ```javascript -
ourmaninamsterdam revised this gist
Jul 30, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ # Array Cheatsheet * [Create an array](#user-content-creating-an-array) * [Get last item](#user-content-getting-last-item) * [Remove first item](#user-content-remove-first-item) * [Remove last item](#user-content-remove-last-item) * [Add new item to beginning](#user-content-add-new-item-to-beginning)
NewerOlder