Skip to content

Instantly share code, notes, and snippets.

@ferdiemmen
Created November 28, 2017 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferdiemmen/c7e5e023155801bd3eae736697d689bf to your computer and use it in GitHub Desktop.
Save ferdiemmen/c7e5e023155801bd3eae736697d689bf to your computer and use it in GitHub Desktop.
Get index by id
const id = 42;
const myArray = [
{
id: 1,
name: 'foobar',
},
{
id: 7,
name: 'lucky',
},
{
id: 42,
name: 'the answer',
}
];
const index = myArray.map(item => item.id).indexOf(id);
// index = 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment