JS – Arrays

One of the most Useful tools that you can find on the tool-box, this is basically a collection of elements, in a variable, imagine a box with subdivisions and index numeration that goes from “0” up to the number of items -1. One place to get good information is the Mozilla Developer Network.

For more METHODS check this Link of the Array.prototype

Article with the: “Most useful ja Array Functions


Arrays:

In JavaScript we use “Sparse Arrays” In computer science, a sparse array is an array in which most of the elements have the default value (usually 0 or null). The occurrence of zero-value elements in a largearray is inefficient for both computation and storage. An array in which there is a large number of zero elements is referred to as being sparse.

One property is that we can have empty elements in between, Elements.

and because this is an Object and the .length is a property you can set it to be what you want with arr.length = 4;  if the number is bigger will create empty container, if is shorter it will delete.

And we can observe the following characteristics:

 

Initialize Arrays

MDN

Testing Array

Notes:
* If there is no property at the key, it should return an empty array.
* If the property at the given key is not an array, it should return an empty array.
* If the array is empty, it should return an empty array.

 

.split()

Creates an Array from a String base on the Parameter that you provide.

.isArray(x)

.length

.indexOf(x)

.lastIndexOf(x)


Access & Modify

Access & Modify

.push()

pushing multiple elements

.unshift()

 .pop()

 .shift()

delete array[x]

This will leave “undefined” in the places where the items were delete, this is good if you want to keep the “array.length“, but if you really want to delete those items and modify the “array.length” use “arr.splice(x, y)

Remove Undefined values from array:

 

.toString()

toString()   —    .replace(/x/g, “y”)

.join(x)

It gives a string representation of the Array, including comas. But you can chose any other separator:    arr.join(” | “);  or nothing at all:  arr.join(“”);

1 Dimensional Array to 2 Dimensional array

 

 

.forEach(x, y, z)

Will run the code of a FUNCTION  on each element of the Array. you can use Anonymous Functions.

it can be used to see how many real elements are in an Array

.                       forEach –>  Object

.reverse()

.concat(x)

.slice(x, y)

Will take a copy of a “Slice” of an array, base on Index instructions   Not changing the original Array.

.splice()

This will remove elements from Array, by Index and return those elements  Will Modify the Original Array.

if we only pass one parameter to .splice

                                 “Adding & Replacing”  elements with  .splice()

                                    Replace

 

.sort()

Text and numbers

Tex, Numbers, Up & Low Cases

Sorting Objects

See how “Haruka Kodoma” is before “Haruka Abcd” lest fix that.

.filter()

Returns an array with all elements that meet every condition in your Function.

filtering an Object

.every()

will return “True” or “False” depending if All elements meet the condition.

now with an Object:

.some()

Will return “True” as long as one element is meet the condition:

.reduce()

This can be used to add all elements in one array, but also you have access to: ( Previous Val, Current Value, index, array) data. form more detail see: MDN .reduce()

in the example we use the following data:

  • pv = Previous Value.
  • cv = Current Value.
  • index = the Index of the current value.
  • array = the complete Array.

It can also concatenate an Array of strings:

Now with an Object: you will see that we are passing a initial value of “0”  and also previous is transform to be a regular variable and not a “.age” to avoid NaN.

 

.reduceRight()

The same as .reduce() but from Right —to–> Left

.map()

Will return an Array with the modify values of each of the Povided Array Values.

.map() —>  UppCase the strings in an Array:

CHAINING METHODS

Taxex & Total

In the following example we have an Array with prices, we want to increment a 6% tax on each, then we want to add them together.

Reverse a “String”

 

.



Copyright 2017. All rights reserved.

Posted January 22, 2017 by Edmundo in category "JS