1.0.12 • Published 2 years ago

objarr-tool v1.0.12

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

objarr-tool

library for working with objects inside an array

Installation

 npm i objarr-tool

Quick start

const ObjarrTool = require('objarr-tool')
const objarr = new ObjarrTool()

To start using, you need to import the ObjarrTool class and instantiate it

Methods

ObjarrTool.filterObjects(array,{keys,values})

const arr = [
    { a: 34, b: 2 },
    { b: 1 },
    { c: 34 },
    { a: 'c', d: 45 }
]

const filteredByValue = objarr.filterObjects(arr, { values: [45] }) // [ { a: 'c', d: 45 } ]
const filteredByKey = objarr.filterObjects(arr, { keys: ['a'] }) // [ { a: 34, b: 2 }, { a: 'c', d: 45 } ]
const filteredByKeyAndValue = objarr.filterObjects(arr, { keys: ['a'], values: [34] }) // [ { a: 34, b: 2 } ]

Filters an array of objects by keys/values, keys and values ​​can be specified together or separately.

As a result of the method execution, a new array will be returned.

ObjarrTool.removeMatches(array)

const arr = [
    { a: null, helloStr: 'hello world' },
    {},
    { a: 34 },
    {},
    { a: null, helloStr: 'hello world' },
    { a: 34 }
]

const uniqueArray = objarr.removeMatches(arr) // [ { a: null, helloStr: 'hello world' }, {}, { a: 34 } ]

Removes duplicate objects

As a result of the method execution, a new array will be returned.

ObjarrTool.getObjValueByKey(key)

const arr = [
    {name:'Anna',age: 20},
    {name:'Jim',age: 60},
    {name:'Daniel'},
    {name:'Anait',age: 16},
    {name:'Diana',age: 30},
]

const namesArray = objarr.getObjValueByKey(arr, 'name') // [ 'Anna', 'Jim', 'Daniel', 'Anait', 'Diana' ]
const agesArray = objarr.getObjValueByKey(arr, 'age') // [ 20, 60, 16, 30 ]

Returns an array from the values ​​of the objects inside the original array by the key given in the arguments

ObjarrTool.sortedByKey(array,key,sortMode)

const arr = [
    {name:'Anna',age: 20},
    {name:'Jim',age: 60},
    {name:'Daniel'},
    {name:'Anait',age: 16},
    {name:'Diana',age: 30},
]

const sortedByName = objarr.sortedByKey(arr,'name')
// [
//     { name: 'Anait', age: 16 },
//     { name: 'Anna', age: 20 }, 
//     { name: 'Daniel' },        
//     { name: 'Diana', age: 30 },
//     { name: 'Jim', age: 60 }
// ]
const sortAscending = objarr.sortedByKey(arr,'age',1)

// [
//     { name: 'Anait', age: 16 },
//     { name: 'Anna', age: 20 },
//     { name: 'Diana', age: 30 },
//     { name: 'Jim', age: 60 }
// ]

const sortDescending = objarr.sortedByKey(arr,'age',-1)

// [
//     { name: 'Jim', age: 60 },
//     { name: 'Diana', age: 30 },
//     { name: 'Anna', age: 20 },
//     { name: 'Anait', age: 16 }
// ]

Sorts the source array by the key given in the arguments.

sortMode can have three meanings:

  • 0 - default collation, matches Unicode code point order
  • 1 - sort key values ​​in ascending order
  • -1 - sort key values ​​in descending order
1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago