Skip to content

Instantly share code, notes, and snippets.

@alvaro893
Created November 16, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvaro893/bbc4ef8a857ce4212247170e8e05a2ca to your computer and use it in GitHub Desktop.
Save alvaro893/bbc4ef8a857ce4212247170e8e05a2ca to your computer and use it in GitHub Desktop.
Javascript utils

Arrays

  1. Introduction
  2. Methods
  3. Snippets

1. Introduction

An array is an object in javascript. It has useful method to play with

2. Methods

Link to array methods

split(separator)

Is the explode php equivalent. turns an string into a array using a separtor

(123456789).toString(10).split("")
["1", "2", "3", "4", "5", "6", "7", "8", "9"]

join(glue)

Is the implode php equivalent. It turn an array into an String

[8, 3, 2, 9, 8, 3, 2].join("-")
"8-3-2-9-8-3-2"

map(function(current))

Method creates a new array with the results of calling a provided function on every element in this array.

push(element)

he push() method adds a new element to an array (at the end). It returns the new array length.

pop()

The pop() method removes the last element from an array. Returns the value that was poped out.

shift()

Shifting is equivalent to popping, working on the first element instead of the last.

unshift(element)

The unshift() method adds a new element to an array (at the beginning).

3. Snippets

Separate an integer into digits in an array

(123456789).toString(10).split("").map(function(t){return parseInt(t)})
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Or

(123456789).toString(10).split("").map(function(t){return Number(t)})
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment