Created
January 15, 2018 20:46
-
-
Save Woodsphreaker/e626b075d3fcbffa74d269d6f0ecd29d to your computer and use it in GitHub Desktop.
Slice Object
This file contains 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 myObj = { | |
key1: 10, | |
key2: 11, | |
key3: 12 | |
} | |
const log = console.log | |
const sliceObject = ( obj, start = 0, end = ( Object.keys(obj).length) ) => { | |
return Object | |
.keys(obj) | |
.slice(start, end) | |
.reduce((acc, el) => { | |
acc[el] = obj[el] | |
return acc | |
}, {}) | |
} | |
log( | |
sliceObject(myObj,0), | |
sliceObject(myObj,2), | |
sliceObject(myObj,0,1), | |
sliceObject(myObj,1,2) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment