Skip to content

Instantly share code, notes, and snippets.

@yckart
Created October 16, 2014 10:39
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 yckart/9ff89179581d6df69782 to your computer and use it in GitHub Desktop.
Save yckart/9ff89179581d6df69782 to your computer and use it in GitHub Desktop.
Helpful utility to get array-items based on indexOf.
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
function take (item, array, context) {
  return array[(context || array).indexOf(item)];
}

var nodes = document.getElementsByTagName('div');
var array = [].slice.call(nodes);
var item = array[2];

var tooked = take(item, array); // => <div>2</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment