Code Monkey home page Code Monkey logo

unwind's Introduction

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')

unwind's People

Contributors

edwincen avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.