Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

javascript-unwind

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

javascript-unwind

A very simple native function for unwinding a collection by a property, like MongoDB's $unwind function.

  • 1.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.1K
increased by18.38%
Maintainers
2
Weekly downloads
 
Created
Source

javascript-unwind

Current npm version Current npm download count CircleCI Compatible with ES5 and greater

A very simple native function for unwinding a collection by a property, like MongoDB's $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

  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:

[ { 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

  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:

[
  {
    "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.

Keywords

FAQs

Package last updated on 03 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc