Arrays in JavaScript
Basic Syntax:
var arrayName = [item0, item1, item2];
An Array can contain multiple types of items
An Array can have an Array as an item.
var Array = ['string1', 5 , [0,1,2], true]
var nums = [1, 1, 2, 3, 5, 8, 13, 21]
for ( var i = 0 ; i < nums.length; i++) {
console.log('in position ' + i + ' there is : ' + nums[i]);
}
in position 0 there is : 1
in position 1 there is : 1
in position 2 there is : 3
in position 3 there is : 5
in position 4 there is : 8
in position 5 there is : 13
in position 6 there is : 21
Array with various types
var Array1 =
[
'someString', //text
5 , //number
[ 0, 'oneMoreString', 2], //array (with multiple types)
true, //boolean
{ name: 'Guy', age: 46} //object with 2 childs
]
Assignment and additions
Array1[1] = 7; //Addition to the array
Array1[4].lastName = 'Maimon'; //addition to the Object
Array1[Array1.length] = [3,4,5,6]; //Addition of an Array to the end
And then
console.log(Array1[Array1.length-1]);
Will result
[3, 4, 5, 6]
the last item in the main array And
console.log(Array1[Array1.length-1][Array1[Array1.length-1].length-1]);
Will result
6
which is the last item in the internal array inside the main array
Arrays useful methods in JavaScript
Array.forEach
arr.forEach(function(theItems, myArrIndex, myArr){}
The Arguments in the function are:
The 1st will return the Items of the Array
The 2nd will return the index of each Items
the 3rd will return the array
Add on or more items to the end of an array
arr.push(item,item);
Delete the last item of an array
arr.pop();
To remove all the items from an array
for (i= 0 ; i < arr.length ; i) { //No Increment needed!!!
arr.pop();
}
Or
while (arr.length >0) {
arr.pop();
}
pop() also returns the removed item
removedItem = arr.pop();
Will return the removed item and assign it to the variable
Add an item to the beginning of an array
arr.unshift(item)
check for place of item in an array
arr.indexOf(item)
will return the 0 based location of an item, if not exist, will return -1
remove multiple items from an array (not last)
arr.splice(startingIntem,NumOfItems);
like pop(), returns the removed item(s) as an variable or array
CAUTION:
if startingNumber smaller than 0 ,
will count from the end of the array!!!
meaning is: if queried and not find ,
will delete the last item!!
More Methods for Arrays
arr.includes(data); //returns a boolean
arr.slice(start,end);
//end not included
//returns the items without deleting from the array
arr3 = arr.concat(arr1,arr2)
firstRemoved = arr.shift()
sortAsc = arr.sort();
reversed = arr.reverse();
changes the original array!!
toCSV = arr.toString()
JS Arrays - Filter,Map,Find,findIndex
arr.filter();
arr.filter(function(array){
return condition
})
var words = ['spray', 'employee', 'limit',
'encyclopedia', 'elite', 'destruct'];
const result = words.filter(
function(word) {
return word.length > 5;
}
);
console.log(result);
will return an array:
["employee", "encyclopedia", "destruct"]
The result will always be an array (even if no results returned)
The function can be extracted from the filter and be represented by a variable
var words = ['spray', 'employee', 'limit',
'encyclopedia', 'elite', 'destruct'];
var myLength = prompt();
var filterType = function(word)
{
return word.length > myLength;
};
var result = words.filter(filterType);
To sreach all the array for all items with some letters inside
var words = ['Spray', 'employee', 'limit', 'encyclopedia', 'elite', 'destruct'];
function filterItems(query){
return words.filter(
function(e1) {
return e1.toLowerCase().indexOf(query.toLowerCase()) > -1
}
)
}
console.log(filterItems(prompt()));
arr.find();
Returns only the first item answering the condition
var nums = [5, 12, 8, 130, 44];
var found = nums.find(
function(element) {
return element > 10;
}
);
console.log(found);
Array.join()
makes a list of values from an array
arr.join(seperator);
Seperator defined as string, default is , (comma)
Arr.map(callBack)
transform the data in an array and create a new array
var nums = [5, 12, 8, 130, 44];
var mapped = nums.map(
function(x){
return x * 2
}
);
console.log(mapped);
Will Return
[10,24,16,260,44]
arr.Reduce
Returns the summ of all items is the array and create a math manipulation with StartNumber
var arr = [1,2,3,4];var reducer = function(acc, StartNumber){
return acc+startNumber;
}
arr.reduce(reducer);
back to main page