https://github.com/noalvo/javascript-unwind
A very simple native function for unwinding a collection by a property, like MongoDB's $unwind function.
https://github.com/noalvo/javascript-unwind
array collection javascript native noalvo object unwind
Last synced: 22 days ago
JSON representation
A very simple native function for unwinding a collection by a property, like MongoDB's $unwind function.
- Host: GitHub
- URL: https://github.com/noalvo/javascript-unwind
- Owner: NOALVO
- License: mit
- Created: 2017-10-14T04:49:29.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2022-02-03T23:24:13.000Z (over 3 years ago)
- Last Synced: 2025-04-15T22:03:54.708Z (about 1 month ago)
- Topics: array, collection, javascript, native, noalvo, object, unwind
- Language: JavaScript
- Size: 40 KB
- Stars: 8
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# javascript-unwind


[](https://circleci.com/gh/NOALVO/javascript-unwind)
A very simple native function for unwinding a collection by a property, like MongoDB's [`$unwind`](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/) function.
## Features
* **This function is immutable.** Your original collection doesn't change.
* **It works for both primitives and complex values**.
* **It works for nested arrays, using dot-notation.** Like `a.b.c`.
* **It works in ES5-compatible environments**, without transpiling.
* **Typings available** for TypeScript.## Installation
```
$ npm i javascript-unwind --save
```## How to use
### Shallow level
```javascript
const unwind = require('javascript-unwind');const collection = [
{ a: [{ x: 1 }, { x: 2 }], b: 123 },
{ a: [{ x: 3 }, { x: 4 }], b: 785, c: 368 },
];console.log(unwind(collection, 'a'));
```Output:
```javascript
[ { a: { x: 1 }, b: 123 },
{ a: { x: 2 }, b: 123 },
{ a: { x: 3 }, b: 785, c: 368 },
{ a: { x: 4 }, b: 785, c: 368 } ]
```### Deep level
```javascript
const unwind = require('javascript-unwind');const collection = [{
name: 'a1',
b: [{
name: 'b1',
c: [ { name: 'c1' }, { name: 'c2' } ]
}, {
name: 'b2',
c: [ { name: 'c3' }, { name: 'c4' } ]
},
],
}, {
name: 'a2',
b: [{
name: 'b3',
c: [ { name: 'c4' }, { name: 'c5' } ]
}, {
name: 'b4',
c: [ { name: 'c6' }, { name: 'c7' } ]
}
]
}
];console.log(unwind(collection, 'b.c'));
```
Output:```javascript
[
{
"name":"a1",
"b":{
"name":"b1",
"c":{
"name":"c1"
}
}
},
{
"name":"a1",
"b":{
"name":"b1",
"c":{
"name":"c2"
}
}
},
{
"name":"a1",
"b":{
"name":"b2",
"c":{
"name":"c3"
}
}
},
{
"name":"a1",
"b":{
"name":"b2",
"c":{
"name":"c4"
}
}
},
{
"name":"a2",
"b":{
"name":"b3",
"c":{
"name":"c4"
}
}
},
{
"name":"a2",
"b":{
"name":"b3",
"c":{
"name":"c5"
}
}
},
{
"name":"a2",
"b":{
"name":"b4",
"c":{
"name":"c6"
}
}
},
{
"name":"a2",
"b":{
"name":"b4",
"c":{
"name":"c7"
}
}
}
]
```## Roadmap and Bugs
See [Issues](https://github.com/NOALVO/javascript-unwind/issues).