Last active
December 16, 2015 19:50
-
-
Save johnsmith17th/5487897 to your computer and use it in GitHub Desktop.
Convert array to object, each element of array is as a key in object, and ignores the non-string elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
/** | |
* Convert array to object, | |
* each element of array is as key in object, | |
* and ignores the non-string elements. | |
* | |
* @example | |
* [ 'a', 'b', 'c' ] => { 'a':1, 'b':1, 'c':1 } | |
* | |
* @param arr{Array} | |
* @returns {Object} | |
*/ | |
function objectify(arr) { | |
var s = {}, x; | |
if (util.isArray(arr)) { | |
for (var i = 0; i < arr.length; i++) { | |
x = arr[i]; | |
if ('string' == typeof(x)) { | |
s[x] = 1; | |
}; | |
}; | |
}; | |
return s; | |
}; | |
//test | |
//console.log(objectify(['a', 'b', 'c'])); | |
module.exports = objectify; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment