Skip to content

Instantly share code, notes, and snippets.

@haipeng
Created September 27, 2017 02:51
Show Gist options
  • Save haipeng/e1957a8aa84463d4d943918d8ab10a11 to your computer and use it in GitHub Desktop.
Save haipeng/e1957a8aa84463d4d943918d8ab10a11 to your computer and use it in GitHub Desktop.
根据字符串、字符串数组创建map
/**
* 根据字符串、字符串数组创建map
* @param {String|Array<String>} items
* @param {String|RegExp} delimiter 默认值/\s+/
* @param {Object} [map]
* @returns {{*}}
*/
var makeMap = function (items, delimiter, map) {
items = items || [];
if (typeof items === 'string') {
items = items.split(delimiter || /\s+/);
}
map = map || {};
for (var i = items.length - 1; i >= 0; i--) {
map[items[i]] = true;
}
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment