array-lib

0.2.0 • Public • Published

array-lib

A library for array functions in Array.prototype

Github

Installation

npm install array-lib

Usage


 const arrayLib = require("array-lib");

clone
deep copy an array


let arr = [1,2,3];
let clone_arr = arr.clone();

print


arr.print();//=> [1,2,3]
"hello".print();// =>hello
(123).print();//=>123
{a:12}.print();//=>{a:12}
 

max
Retrun max value from array


arr.max().print();

maxIndex
Returns index of max value in array:


arr.maxIndex().print()

remove
Removes element at the specified position


arr.remove(0);
 
arr.print()//=> [2,3]

removeVal
Removes first or all values from array


let a = [1,2,3,4,4,5,5];
let remove_all_elements = true;
a.removeVal(4,remove_all_elements);
 
a.print();//=> [1,2,3,5,5];

count
Count how much times a element is in an array


let b = ["a","b","b","c","b"];
console.log(b.count("b"));//=> 3

replace
Replace elements in array


let c = [1,2,3,3,3,4,6,6,9];
c.replace(3,"d");
 
c.print();//=>[1,2,"d","d","d",4,6,6,9]

addUp
Add up all numbers in array


let d = [1,2,3,4,5];
 
d.addUp().print();//=> 15

matrix
Create a 2d array


let arr = Array.matrix(2,3,"fill")
arr.print();
//=>
//
//    [ 'fill', 'fill', 'fill' ],
//    [ 'fill', 'fill', 'fill' ]
// ]

vector
Create an array


let arr = Array.vector(6,"fill");
arr.print();//=> ['fill', 'fill', 'fill','fill', 'fill', 'fill']

toArray
Create an array from a string


"hello ,world ,how ,are ,you".toArray().print();//=>
 //[ 'hello', 'world', 'how', 'are', 'you' ]

multiplyMatrices


let b = [[1,2],[3,4]]
b.multiplyMatrices([[1,4],[6,7]]).print()
//=>[ [ 13, 18 ], [ 27, 40 ] ]

isMatrix
check if an array is a 2d matrix


 let matrix = [
     [0,1,2],
     [3,4,5],
     [6,7,8]
 ];
 
 matrix.isMatrix().print()// => true

avrage
get Avrage from array


    [1,2,3,4,5].avrage().print();//=> 3

pos
return first position of element in an array


  [1,2,3,4,5].pos(4).print();//=>3
  [1,2,3,4,5].pos(9).print();//=>false

getAllPos
Get all positions of element in array


let arr = [1,3,4,4,8,7,5,3,4];
b.getAllPos(4).print();//=> [2,3,8]

shuffle
shuffle an array


  [1,2,3,4,5,6,7,8,9].shuffle().print();
  //=> [ 7, 2, 9, 1, 6, 8, 3, 5, 4 ]

range
A function to create flexibly-numbered lists of integers


Array.range(10);
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array.range(1, 11);
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array.range(0, 30, 5);
//=> [0, 5, 10, 15, 20, 25]
Array.range(0, -10, -1);
//=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
Array.range(0);
//=> []

without
Returns a copy of the array with all instances of the values removed.

let a = [1,2,3,1,2,31,2,31,1,2,3,]
let b = a.without(1,2);
b.print();
  //=>[ 3, 31, 31, 3 ]

Package Sidebar

Install

npm i array-lib

Weekly Downloads

4

Version

0.2.0

License

ISC

Unpacked Size

11.8 kB

Total Files

3

Last publish

Collaborators

  • dovid_crow