https://github.com/aleclarson/slice-object
Slice an object into a new array ⚡️
https://github.com/aleclarson/slice-object
arguments arrays slice
Last synced: 11 months ago
JSON representation
Slice an object into a new array ⚡️
- Host: GitHub
- URL: https://github.com/aleclarson/slice-object
- Owner: aleclarson
- License: mit
- Created: 2018-07-16T19:59:28.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-05T16:57:58.000Z (over 7 years ago)
- Last Synced: 2025-06-19T12:54:22.164Z (about 1 year ago)
- Topics: arguments, arrays, slice
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slice-object v1.2.0
Fastest way to slice an object into a new array.
Almost identical to `[].slice`, yet faster in all cases. The only difference: holes are filled in, which means sparse arrays are never returned. You can even use this function to convert a sparse array into a packed array!
```typescript
slice(obj: Object, start: number = 0, end: number = obj.length) : Array
```
### Usage
```js
const slice = require('slice-object');
// Works with any object that has a `length` property.
const obj = {
0: 0,
1: 1,
2: 2,
length: 3,
};
slice(obj); // => [0, 1, 2]
slice(obj, 1); // => [1, 2]
slice(obj, 1, 2); // => [1]
// Perfect for `Arguments` objects.
function args() { return arguments }
slice(args(1, 2, 3, 4), 2); // => [3, 4]
// Slicing arrays is faster, too!
slice([1, 2, 3], 1); // => [2, 3]
// When the start index is >= the length, an empty array is returned.
slice(args(1, 2), 2); // => []
// When the start index is >= the end index, an empty array is returned.
slice(args(1, 2), 1, 1); // => []
// Negative indexes are supported.
slice(args(1, 2, 3), -2, -1); // => [2]
// Create a packed array from a sparse array.
const arr = [];
arr[1] = 1;
arr.hasOwnProperty(0); // => false
slice(arr).hasOwnProperty(0); // => true
```