Created
December 7, 2017 21:13
-
-
Save farskid/4f0c5f7d64cc79babc341c52baebc567 to your computer and use it in GitHub Desktop.
Create a key value object from a 2d array in javascript
This file contains hidden or 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
/* | |
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] | |
} | |
}; | |
}, {}); | |
} |
This file contains hidden or 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
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