group-objects-array
Advanced tools
@@ -21,2 +21,3 @@ "use strict"; | ||
| * let arr = [{name: "John", age: 30}, {name: "Doe", age: 29}, {name: "John", age: 30}, {name: "Michel", age: 21}]; | ||
| * | ||
| * uniqueArray(arr); | ||
@@ -23,0 +24,0 @@ * // returns [{name: "John", age: 30}, {name: "Doe", age: 29}, {name: "Michel", age: 21}] |
+12
-4
| { | ||
| "name": "group-objects-array", | ||
| "version": "1.0.1", | ||
| "description": "\"Groups and merges array of objects by a single key\"", | ||
| "version": "1.0.2", | ||
| "description": "Groups and merges array of objects by a single key.", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| "test": "node ./tests/groupObjectArrayByKey.spec.js" | ||
| }, | ||
@@ -16,3 +16,11 @@ "keywords": [ | ||
| "author": "Jana Rangasamy <janagr7@gmail.com>", | ||
| "license": "ISC" | ||
| "license": "ISC", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/grjan7/group-objects-array.git" | ||
| }, | ||
| "homepage": "https://github.com/grjan7/group-objects-array#readme", | ||
| "bugs": { | ||
| "url": "https://github.com/grjan7/group-objects-array/issues" | ||
| } | ||
| } |
+58
-9
| # group-objects-array | ||
| Groups JavaScript objects array by a key and returns grouped array. | ||
@@ -6,5 +7,6 @@ | ||
| 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. | ||
| 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 | ||
| ```sh | ||
@@ -16,4 +18,10 @@ | ||
| ## Example: | ||
| ## Usage: | ||
| ### `groupObjectArrayByKey(objArr, groupByKey)` | ||
| - **objArr** an array of objects to be grouped | ||
| - **groupByKey** an object key to group the objects | ||
| ```js | ||
@@ -23,3 +31,3 @@ | ||
| let objArray = [ | ||
| const objArray = [ | ||
| {id: 1, name: "John"}, | ||
@@ -30,3 +38,3 @@ {id: 2, name: "Aaron"}, | ||
| {id: 3, name: "Michel"}, | ||
| {id: 1, address: {street: "123 Main Street", city: "NY", country: "USA"}} | ||
| {id: 1, address: {street: "123 Main Street", city: "NY", country: "USA"}}, | ||
| {id: 3, contact: "+01-51245 53125"}, | ||
@@ -38,11 +46,52 @@ {id: 3, contact: "+02-51245 53125"}, | ||
| groupObjectArrayByKey(objArray, "id"); | ||
| //returns | ||
| ``` | ||
| returns | ||
| ```js | ||
| [ | ||
| {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"]} | ||
| {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 | ||
| ```js | ||
| 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 | ||
| ```js | ||
| [{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 | ||
| ```js | ||
| const { isItemInArray } = require("group-objects-array"); | ||
| const arr = [{name: "John"}, "world"]; | ||
| isItemInArray(arr, {name: "John"}); // returns true | ||
| ``` |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
52731
2.47%468
0.21%0
-100%0
-100%92
109.09%0
-100%