lodash-unwind

1.2.3 • Public • Published

lodash-unwind

Build Status Greenkeeper badge JavaScript Style Guide

Very simple unwind extension for lodash. And it's compatible with mongoose doc.

Install

npm install --save lodash-unwind

Usage

unwind(collection, path, [options={}])

Arguments

  • collection(Array|Object) - The collection or object to process.
  • path(string) - The property path to unwind.
  • [options={}](object) - The option object.
  • [options.ignoreNonArray=true](boolean) - Specify whether ignore non-array element/property, default=true.
  • [options.wrapAsArray=false] - Specify whether wrap unwinded value in array.

Returns

(Array) - Returns new unwinded collection.

Sample

Unwind normal collection

const unwind = require('lodash-unwind')()
const data = [
  {
    a: [ 1, 2 ],
    id: 'a1'
  },
  {
    a: [ 3, 4 ],
    id: 'a2'
  }
]
const output = unwind(data, 'a')
// [
//   {
//     a: 1,
//     id: 'a1'
//   },
//   {
//     a: 2,
//     id: 'a1'
//   },
//   {
//     a: 3,
//     id: 'a2'
//   },
//   {
//     a: 4,
//     id: 'a2'
//   }
// ]

Unwind collection with nested property

const unwind = require('lodash-unwind')()
const data = [
  {
    a: {
        inner: [ 1, 2 ]
    },
    id: 'a1'
  },
  {
    a: {
        inner: [ 3, 4 ]
    },
    id: 'a2'
  }
]
const output = unwind(data, 'a')
// [
//   {
//     a: { inner: 1 },
//     id: 'a1'
//   },
//   {
//     a: { inner: 2 },
//     id: 'a1'
//   },
//   {
//     a: { inner: 3 },
//     id: 'a2'
//   },
//   {
//     a: { inner: 4 },
//     id: 'a2'
//   }
// ]

Unwind object

const unwind = require('lodash-unwind')()
const data = {
  a: [ 1, 2 ],
  id: 'a1'
}
const output = unwind(data, 'a')
// [
//   {
//     a: 1,
//     id: 'a1'
//   },
//   {
//     a: 2,
//     id: 'a1'
//   }
// ]

Unwind with wrapAsArray=true

const unwind = require('lodash-unwind')()
const data = {
  a: [ 1, 2 ],
  id: 'a1'
}
const output = unwind(data, 'a', { wrapAsArray: true })
// [
//   {
//     a: [1],
//     id: 'a1'
//   },
//   {
//     a: [2],
//     id: 'a1'
//   }
// ]

Use unwind as module of lodash

const _ = require('lodash')
require('lodash-unwind')({ injected: true })
const data = [
  {
    a: [ 1, 2 ],
    id: 'a1'
  },
  {
    a: [ 3, 4 ],
    id: 'a2'
  }
]
// Use unwind as part of lodash
const output = _.unwind(data, 'a')

Readme

Keywords

Package Sidebar

Install

npm i lodash-unwind

Weekly Downloads

92

Version

1.2.3

License

MIT

Unpacked Size

12.5 kB

Total Files

7

Last publish

Collaborators

  • edwincen