Made something useful. It's a simple utility package to help deal with arrays and an array ...

栏目: IT技术 · 发布时间: 5年前

内容简介:A lightweight library containing multiple utility functions to make your life easier when dealing with arrays and objects.

Simplifying JS

Made something useful. It's a simple utility package to help deal with arrays and an array ...

A lightweight library containing multiple utility functions to make your life easier when dealing with arrays and objects.

HOW TO USE

Open the terminal and navigate to the root directory of your project.

Install Simplifying:

npm i simplifying

If you do not have npm installed, you can download it here .

EXAMPLE CODE:

const array = require('simplifying');
let arr = [2, 3, 4, 3, 3, 2, 4];
console.log(array.removeDuplicates(arr));

//OUTPUT: [2, 3, 4]

List of all methods on array:

Method Parameters Use
shuffle array: Array Shuffles array and returns it
removeDuplicates array: Array Removes all duplicate elements and returns a new array
pickRandom array: Array,
n: Number,
unique?: Boolean
Returns an array of n random elements from array.
The returned elements will be unique if true is passed in the third parameter
countItem array: Array,
item: any
Returns the frequency of the passed item in the array
countAll array: Array Returns a map which holds the frequencies of all items in the array
searchItems array: Array,
item: any
Returns all positions where the given item is found
removeItems array: Array,
item: any
Deletes multiple occurances of the given item
toArray x: (string, number) Converts string or number to array of characters or digits
merge array1: Array,
array2: Array,
type?: string
Merge two arrays.
type == 'merge-all': Keep the duplicates
type == 'common': Returns common elements
no type: Merge and remove common
clean array: Array Removes null, undefined, '', NaN
flatten array: Array Flattens all nested arrays inside
swapByPosition array: Array,
pos1: number,
pos2: number
Swaps values in pos1 and pos2 in the array
swapByValues array: Array,
value1: any,
value2: any
Swaps value1 and value2 in the array
perform array: Array,
operation: string
Performs the said operation on the array.
Allowed Operations:
sum, product, min, max, average
arrayToObject array: Array Converts an array to object
slide array: Array,
steps: number,
direction?: string
Shifts array by n steps in the given direction. The default direction is left.
range array: Array,
start: number,
end: number
Returns a subarray from start to end - 1

List of all methods on array of objects:

Method Parameters Use
arrayOfObjectsToObject array: Array,
keyField: string
Converts an array of objects to an object where key is keyField
sortByKeyField array: Array,
keyField: string,
order?: string
Sorts an array of objects based on keyField. Default sorting order is ascending. Pass 'desc' to order for reverse.
getFields array: Array,
fields: Array
Returns an array of objects containing only the required fields
getOneField array: Array,
field: string
Returns an array of all the values of the particular field
searchByKeyField array: Array,
searchField: string,
searchValue: string,
reqFields?: Array
Searches the array for the given (key, value) pair and returns only the reqFields. If nothing is passed, the whole object is returned.
deleteByKeyField array: Array,
field: string,
value: string
Deletes an item based on the (key, value) pair
getKeys array: Array Returns an array of all the keys inside the object. Assumes that all objects have the same keys
findObjects array: Array,
func: Function
Executes the function on all objects and returns the satisfied objects

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

群体智能

群体智能

James Kennedy、Russell C Eberhart、Yuhui Shi / 人民邮电出版社 / 2009-2-1 / 75.00元

群体智能是近年来发展迅速的人工智能学科领域.通过研究分散,自组织的动物群体和人类社会的智能行为, 学者们提出了许多迥异于传统思路的智能算法, 很好地解决了不少原来非常棘手的复杂工程问题.与蚁群算法齐名的粒子群优化(particle swarm optimization, 简称PSO)算法就是其中最受瞩目,应用最为广泛的成果之一. 本书由粒子群优化算法之父撰写,是该领域毋庸置疑的经典著作.作者......一起来看看 《群体智能》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具