4dev-lib-array
v1.0.2
Published
> For working with objects and arrays
Downloads
13
Maintainers
Readme
4dev-lib-array
For working with objects and arrays
List of methods:
- merge: Merging two objects
- search_value: Finding an object by value
- clear_empty: Removing empty objects or keys
- sort_arr: Sorting an object by key
- obj_key_to_array: Converting an Object Key to an Object
- array_diff: Will return the difference between the arrays
Installation
$ npm install --save 4dev-lib-array
Usage
Examples of using
var fd_lib_array = require('4dev-lib-array')
async function test()
{
//Merging two objects
var obj1 = {}
obj1[0] = {}
obj1[0]['id'] = "1"
obj1[0]['title'] = "test text"
var obj2 = {}
obj2[0] = {}
obj2[0]['id'] = "1"
obj2[0]['title'] = "test text 2"
obj2['test'] = true
console.log("test: 4dev-lib-array.merge")
var r = await fd_lib_array.merge(obj1,obj2)
console.log(r)
//Finding an object by value
var obj1 = {}
obj1[0] = {}
obj1[0]['id'] = "1"
obj1[0]['title'] = "test text"
console.log("test: 4dev-lib-array.search_value")
var r = await fd_lib_array.search_value(obj1, "1")
console.log(r)
//Removing empty objects or keys
var obj1 = {}
obj1[0] = {}
obj1[0]['id'] = "1"
obj1[0]['title'] = ""
obj1[1] = {}
console.log("test: 4dev-lib-array.clear_empty")
var r = await fd_lib_array.clear_empty(obj1)
console.log(r)
//Sorting an object by key
var obj1 = {}
obj1[0] = {}
obj1[0]['id'] = "1"
obj1[0]['title'] = "title 1"
obj1[1] = {}
obj1[1]['id'] = "2"
obj1[1]['title'] = "title 2"
console.log("test: 4dev-lib-array.sort_arr")
var r = await fd_lib_array.sort_arr(obj1, "id", "<")
console.log(r)
//Converting an Object Key to an Object
var obj1 = {}
obj1['key[1][2]'] = '2'
console.log("test: 4dev-lib-array.obj_key_to_array")
var r = await fd_lib_array.obj_key_to_array(obj1)
console.log(r)
//Will return the difference between the arrays
var array1 = ['a', 'b'];
var array2 = ['a', 'b', 'c', 'd'];
console.log("test: 4dev-lib-array.array_diff")
var r = await fd_lib_array.array_diff(array1,array2)
console.log(r)
}
test()
License
ISC © Denis