Last active
May 22, 2020 09:28
-
-
Save byverdu/869f6735a56c45967bf705d98e591b50 to your computer and use it in GitHub Desktop.
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
const allInputs = document.querySelectorAll( 'a' ); | |
const urlToSearch = 'https://github.com/byverdu'; | |
let found; | |
let position; | |
// Ways to iterate over a NodeList to find an element | |
// 1 | |
allInputs.forEach(( link, index ) => { | |
if (link.href.indexOf( urlToSearch ) !== -1 ) { | |
position = index; | |
} | |
}); | |
found = allInputs[ position ]; | |
// 2 | |
for ( let i = 0; i <= allInputs.length; i++ ) { | |
if (allInputs[ i ].href.indexOf( urlToSearch ) !== -1 ) { | |
position = index; | |
} | |
} | |
found = allInputs[ position ]; | |
// 3 | |
found = [].find.call( allInputs, link => link.href === urlToSearch ); | |
// 4 | |
found = Array.from( allInputs ).find( link => link.href === urlToSearch ); | |
//5 | |
found = [...allInputs].find(link => link.href === urlToSearch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment