Skip to content

Instantly share code, notes, and snippets.

@srph
Created May 26, 2017 10:45
Show Gist options
  • Select an option

  • Save srph/943081f31559c4cfe9bdb2f28d536acd to your computer and use it in GitHub Desktop.

Select an option

Save srph/943081f31559c4cfe9bdb2f28d536acd to your computer and use it in GitHub Desktop.
JS: Array to object
/**
* Converts array to object with sparse values.
* Useful when for example, using a dictionary to set mass ids to a value
* { loading: { 1: true, 4: true } }
*
* @param {array} array Primitive values
* @return {object}
*/
export default function objectify(array, value) {
const result = {};
array.forEach(key => {
result[key] = value;
})
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment