Arrays

Working with arrays

example:

var list = [1, 2, 3, 4, 5, 6, 7, 8, 9];


getting value in array

example:

var list = [1, 10, 3, 4, 5, 6, 7, 8, 9]; 
list[3];

--> 4

assigning numbers in array

example:

var[5] = "test";
var list = [1, 10, 3, 4, 5, "test" , 7, 8, 9];

adjusting numbers in array

example:

var list = [1, 10, 3, 4, 5, 6, 7, 8, 9];

Now we want to change the number 10 into number 2:

list[1] = "2"

//this will make the outcome like this: 

var list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

reversing information in array

example:

var list = [1, 10, 3, 4, 5, 6, 7, 8, 9];

//using .reverse(); will reverse the array

var list = [1, 10, 3, 4, 5, 6, 7, 8, 9];
var reverseList = list.reverse(); 
console.log(reverseList);

--> [9, 8, 7, 6, 5, 4, 3, 10, 1]

Deleting last position in array

list.pop();

//this will delete the last position: 

var list = [1, 2, 3, 4, 5, 6, 7, 8];

you can store the last data into a variable

var newList = list.pop();
console.log(newList);

--> 9

Deleting first position in array

list.shift();

//this will delete the first position: 

var list = [2, 3, 4, 5, 6, 7, 8, 9];

you can store the first data into a variable

var newList = list.shift();
console.log(newList);

--> 1

Adding a position in array

list.push("10");

//this will add anorher position: 

var list = [1, 2, 3, 4, 5, 6, 7, 8, 10];

Calling positions in several arrays

var list = [1, 2, 3, 4, 5];
var newlist = [6, 7, 8, 9];
var listingPosition = [list, newlist];

We want to create a new variable "position" that is storing number 9

var position = listingPosition[1][3];

getting length in array

var list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
list.length();

--> 9

joining the array with .join( );

var name = ["John", "Doe"];
var full = name.join();
console.log(full);

-->John,Doe

var name = ["John", "Doe"];
var full  =  name.join(" ");
console.log(full);

-->John Doe

var name = ["John", "Doe"];
var full  =  name.join("and");
console.log(full);

-->John and Doe

returning full array with .map( );

list.map();

var list = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
list.map(function(num){
  return num * 10; 
});
console.log(result);

--> [10, 20, 30, 40, 50, 60, 70, 80, 90]

2D arrays

var list = [ [3,5] , [4, 5] ];

jagged arrays (unequal array)

var list = [ [3,4,5] , [4], [7,8] ];

results matching ""

    No results matching ""