Skip to content

Instantly share code, notes, and snippets.

@qunabu
Created January 14, 2020 20:59
Show Gist options
  • Save qunabu/4df76c31c8cbb90aa899dcdd89ba98d0 to your computer and use it in GitHub Desktop.
Save qunabu/4df76c31c8cbb90aa899dcdd89ba98d0 to your computer and use it in GitHub Desktop.
getTranslationHelpers.js
/**
* Converts object to array
* @param {Object} obj
* @returns {Array}
* @example
*
* convertToArray({fullName:"Full Name"}) // returns [{key:"fullName", value:"Full name"}]
*/
const convertToArray = obj =>
Object.keys(obj).map(key => ({ key, value: obj[key] }));
/**
* Converts array to object
* @param {Array} obj
* @returns {Object}
* @example
*
* convertToObject([{key:"fullName", value:"Full name"}]) // returns {fullName:"Full Name"}
*/
const convertToObject = arr =>
arr.reduce((acc = {}, curr) => ({ ...acc, [curr.key]: curr.value }), {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment