https://github.com/dy/array-rearrange
Rearrange array elements by index
https://github.com/dy/array-rearrange
Last synced: 9 months ago
JSON representation
Rearrange array elements by index
- Host: GitHub
- URL: https://github.com/dy/array-rearrange
- Owner: dy
- License: other
- Created: 2018-02-08T23:46:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-08-18T06:09:07.000Z (almost 4 years ago)
- Last Synced: 2025-08-20T10:34:26.468Z (10 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# array-rearrange [](http://github.com/badges/stability-badges) [](https://travis-ci.org/dfcreative/array-rearrange)
Reorder elements within array by index.
[](https://npmjs.org/package/array-rearrange/)
```js
const reorder = require('array-rearrange')
let arr = reorder([9,8,7,6], [3,2,1,0]) // [6,7,8,9]
let arr2 = reorder([3,3, 2,2, 1,1], [2,1,0]) // [1,1, 2,2, 3,3]
```
## API
### array = reorder(array, index, stride=1)
Shuffle elements in `array` according to the `index` array passed. Permutes original array. `index` array should contain unique indexes. Pass `stride` to indicate groups of elements to shuffle.
Originally based on the [reference solution](https://www.geeksforgeeks.org/reorder-a-array-according-to-given-indexes/), which turns out to be wrong, so algorithm is fully rewritten.
## See also
* [array-normalize](https://github.com/dfcreative/array-normalize)
* [array-bounds](https://github.com/dfcreative/array-bounds)
* [array-same](https://github.com/dfcreative/array-same)
* [array-polar](https://github.com/dfcreative/array-polar)
* [negative-index](https://github.com/dfcreative/negative-index)
* [deinterleave](https://github.com/dfcreative/deinterleave)