Skip to content

Instantly share code, notes, and snippets.

@yckart
Created Oct 16, 2014
Embed
What would you like to do?
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