Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active November 30, 2016 22:11
Show Gist options
  • Save akirattii/4bcb397009d6aacdd412e88791453181 to your computer and use it in GitHub Desktop.
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.
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