Skip to content

Instantly share code, notes, and snippets.

@aamnah
Last active January 2, 2017 16:52
Show Gist options
  • Select an option

  • Save aamnah/42627fa15bb1c283c5ea7d89493923b1 to your computer and use it in GitHub Desktop.

Select an option

Save aamnah/42627fa15bb1c283c5ea7d89493923b1 to your computer and use it in GitHub Desktop.
Map vs. For Loop
/*
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