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

join-by-keys

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

join-by-keys

Join an array of objects on a common key or keys.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

join-by-keys

Join an array of objects on a common key or keys.

Build Status npm install

Example

var join = require('join-by-keys')

var result
  , data

data = [
    {
      'i': 0
      'j': 0
      'v': 1
    }
  , {
      'i': 0
      'j': 0
      'v': 2
    }
  , {
      'i': 1
      'j': 0
      'v': 3
    }
  , {
      'i': 1
      'j': 1
      'v': 4
    }
  , {
      'i': 1
      'j': 1
      'v': 5
    }
]

join(data, ['i', 'j']) 
// returns:
// [
//     {i: 0, j: 0, v: [1, 2]}
//   , {i: 1, j: 0, v: [3]}
//   , {i: 1, j: 1, v: [4, 5]}
// ]

API

The module exports a function:


function(data, keys) -> joinedData

Arguments

  • data is assumed to be an array of JavaScript objects to join.
  • keys is an array of strings representing the names of attributes whose values should be used to join the data

Return value

joinedData is an array of JavaScript objects: The join of the data on the specified keys. It is computed as follows:

  • Find all the elements of data with the same value in each of the keys slots.
  • Create a new object representing the join of the matching elements by doing the iterating over each key and applying the following rules:
    • If key is in keys, on the new object, set the key to the value of the first matching object. (It doesn't matter which you choose, since they are all the same anyway).
    • If key is not in keys, in the returned object, set key to an array holding all the values for the given key found on all of the matching objects.

License

This project is licensed under the Apache License, Version 2.0. See LICENSE for the full license.

Keywords

FAQs

Package last updated on 15 May 2015

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