Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Created January 15, 2018 20:46
Show Gist options
  • Save Woodsphreaker/e626b075d3fcbffa74d269d6f0ecd29d to your computer and use it in GitHub Desktop.
Save Woodsphreaker/e626b075d3fcbffa74d269d6f0ecd29d to your computer and use it in GitHub Desktop.
Slice Object
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