Skip to content

Latest commit

 

History

History

Array-Methods

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Quick Overview

Javascript arrays are iterable collections containing variable data types and resizable as per requirement. They inherit from Array.prototype which is it's object type. It provides a number of useful members for various operations on these objects.

Creation

  • new   Array(...arg)    length (single arg) or values (multiple args)
  • Array.from(...itr)      shallow copy of any iterable
  • Array.of(...values)      expands and combines all values

General Property/Methods

  • arr.length
  • arr.at
  • arr.sort( comp_fun(first,second) )    Takes reference to a function, sorts in occurence order if returned value > 0 , otherwise reverse order
  • arr.reverse()    In place reversal of original order

Mutation

  • arr.push()     append end
  • arr.pop()      remove last
  • arr.unshift(...args)   add multiple beginning
  • arr.shift()      remove first
  • arr.splice( start,del_count,...items )    starting at given index delete any no. or add any no. of new elements
  • arr.concat(...items)    appends multiple values or arrays (merge)
  • arr.fill( val )      all elements changed to val

Search/Existence

  • arr.indexOf()      index or -1 if not exists
  • arr.find( test_fun )      first match (element) passing callback or undefined
  • arr.findIndex( test_fun )    index of first callback pass or -1
  • arr.findLast( test_fun )      last test function pass(element) or undefined
  • arr.findLastIndex( test_fun )    index of last callback pass or -1

Conversion

  • arr.join( sep )      Array to String with elements concatenated using seperator | default : ','
  • arr.toLocaleString()      array to string including commas
  • str.split( sep )    string to array using separator to split into elements

Iteration

  • arr.forEach( callback )    iterates over array items & executes callback with args - element,index & array
  • arr.values()    returns an iterator to object values [non-mutating]
  • str.keys()    returns iterator to object keys/indices
  • arr.entries()    returns iterator containing both keys & values as first & second items respectively

Subseting / Testing

  • arr.slice( start,end )    sub-array from start upto end (non including) [non-mutating] [shallow-copy]
  • arr.filter( callback )    sub-array comprising elements passing callback check [n-m] [s-c]
  • arr.includes( val )    boolean representing existence of item
  • arr.every( test_fun )    boolean representing test function check value by all items (always true for empty arr)
  • arr.some( test_fun )    boolean representing test function pass by atleast 1 item (always false for empty arr)

Transformation

  • arr.map( callback )    iterate over each item and returns new array with elements corresponding to values returned by callback in order [n-m]
  • arr.reduce( ( prevVal,el,ind,arr)=> ... , initVal )    reducer callback aggregating results of execution previous item, starts iterating from index 1 with 0th element as initVal if not provided explicitly