Last active
November 30, 2016 22:11
-
-
Save akirattii/4bcb397009d6aacdd412e88791453181 to your computer and use it in GitHub Desktop.
How to get an array's index which object has a certain property value.
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
| let items = [ | |
| {"id": "aaa"}, | |
| {"id": "bbb"}, | |
| {"id": "ccc"}, | |
| {"id": "bbb"}, | |
| ]; | |
| let idx; | |
| idx = items.findIndex(x => x.id == "aaa"); // idx => 0 | |
| idx = items.findIndex(x => x.id == "bbb"); // idx => 1 // returns idx of "bbb" first found | |
| idx = items.findIndex(x => x.id == "ccc"); // idx => 2 | |
| idx = items.findIndex(x => x.id == "xxx"); // idx => -1 // returns -1 if not found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment