Skip to content

Instantly share code, notes, and snippets.

@farskid
Created December 7, 2017 21:13
Show Gist options
  • Save farskid/4f0c5f7d64cc79babc341c52baebc567 to your computer and use it in GitHub Desktop.
Save farskid/4f0c5f7d64cc79babc341c52baebc567 to your computer and use it in GitHub Desktop.
Create a key value object from a 2d array in javascript
/*
const array = [
['name', 'some name'],
['size', 1746],
['format', 'some format']
];
convert array to an object as:
{
name: 'some name',
size: 1746,
format: 'some format'
}
*/
function convert2DArrayToKeyValueObject(array) {
return array.reduce((result, innerArray) => {
return {
...result,
...{
[innerArray[0]]: innerArray[1]
}
};
}, {});
}
function splitByKeyValue(locationString, separator) {
return locationString.split(separator);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment