Git Product home page Git Product logo

group-objects-array's Introduction

group-objects-array

NPM version NPM downloads Known Vulnerabilities

Description

Groups JavaScript objects array by a key and returns grouped array.

Groups the scattered objects in an array based on a groupByKey (e.g. id). For the given groupByKey value, if there is a multiple occurrence of same key (e.g., contact key for id: 3) but with unique values, then the values will be grouped into an array.

Installation

  npm i group-objects-array

Usage

groupObjectArrayByKey(objArr, groupByKey)

  • objArr an array of objects to be grouped
  • groupByKey an object key to group the objects
 const { groupObjectArrayByKey } = require('group-objects-array');
 
 const objArray = [
   { id: 1, name: "John" },
   { id: 2, name: "Aaron" },
   { id: 1, age: 20 },
   { id: 2, age: 30 },
   { id: 3, name: "Michel" },
   { id: 1, address: { street: "123 Main Street", city: "NY", country: "USA" } },
   { id: 3, contact: "+01-51245 53125" },
   { id: 3, contact: "+02-51245 53125" },
   { id: 1, address: { street: "123 Main Street", city: "NY", country: "USA" } }  
 ];
  
 groupObjectArrayByKey(objArray, "id");
 

returns

[
  {
    id: 1, 
    name: "John", 
    age: 20, 
    address: { street: "123 Main Street", city: "NY", country: "USA" }
  },
  {
    id: 2,
    name: "Aaron",
    age: 30
  },
  {
    id: 3,
    name: "Michel",
    contact: [ "+01-51245 53125", "+02-51245 53125" ]
  } 
]

uniqueArray(arr)

This function returns an array with duplicates removed.

  • arr an array to be deduplicated to have only unique values
const { uniqueArray } = require("group-objects-array");

const arr = [
  { name: "John", age: 30 },
  { name: "Doe", age: 29 },
  { name: "John", age: 30 },
  { name: "Michel", age: 21 }
];

uniqueArray(arr);

returns

[
  { name: "John", age: 30 },
  { name: "Doe", age: 29 },
  { name: "Michel", age: 21 }
]

isItemInArray(arr, item)

This function checks if an item is in the given array.

  • arr source array
  • item item to be checked whether it is in the array
const { isItemInArray } = require("group-objects-array");

const arr = [ { name: "John" }, "world" ];

isItemInArray(arr, { name: "John" }); // returns true

group-objects-array's People

Contributors

grjan7 avatar

Watchers

 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.