Skip to content

Instantly share code, notes, and snippets.

@netpoetica
Last active August 29, 2015 14:03
Show Gist options
  • Save netpoetica/15f6451f3d10278fad7f to your computer and use it in GitHub Desktop.
Save netpoetica/15f6451f3d10278fad7f to your computer and use it in GitHub Desktop.
enumerate - decompose an array for iteration in a for...in
/*
Usage
// You have an array
var myArr = ["a", 'b', "c"];
// You want to enumerate through it w/ specified index. If you enumerate
// through an array by default index cannot be "guaranteed" as per MSDN:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
// for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.
for (var c in enumerate(myArr)){
console.log(c);
}
*/
// Ideally add to a UTILS object or something.
var enumerate = function(arr){
var len = arr.length,
keys = Object.keys(arr),
result = {};
while(len--){
result[keys[len]] = a[len];
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment