Last active
January 2, 2017 16:52
-
-
Save aamnah/42627fa15bb1c283c5ea7d89493923b1 to your computer and use it in GitHub Desktop.
Map vs. For Loop
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 characters
| /* | |
| Loop over a two-dimensional array | |
| https://jsfiddle.net/7p6g03z6/ | |
| */ | |
| arr = ['Ali', 'Hakim', 'Rana', ['Shahid', 'Naeem']] | |
| // Map and Conditional (ternary) Operator | |
| arr.map(name => { | |
| Array.isArray(name) | |
| ? name.map(nestedName => console.info(nestedName)) | |
| : console.info(name) | |
| }) | |
| // Regular ol' for loops and if statements | |
| for (let i = 0; i < arr.length; i++) { | |
| if ( typeof arr[i] === 'object') { | |
| for (let j = 0; j < arr[i].length; j++) { | |
| console.info(arr[i][j]) | |
| } | |
| } else { | |
| console.info(arr[i]) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment