prisskreative
7/10/2014 - 3:34 AM

Arrays

Arrays

/* Structuring values
In your programming travels you will also have need to structure values into more complex patterns. Let's go over the two basic ways of doing that.

Arrays
An array is a list of values within square brackets [ ]*/

//Structuring values
//In your programming travels you will also have need to structure values into more complex patterns. Let's go over the two basic ways of doing that.

//Arrays
//An array is a list of values within square brackets []. Let's see a quick example:

var countries;

countries = [ "Russia", "Finland", "Morocco", "Brazil" ];

console.log(countries);

/*Once you create your array, you can pass it around and use it 
like any other value. You could also think of an array as a 
row of lockers because each value has a number position assigned to it, 
starting with 0. You can access each position's value by using square brackets [ ]*/

var countries;

countries = [ "Russia", "Finland", "Morocco", "Brazil" ];

// Russia
console.log("The first country is " + countries[0] + "!");

// Finland
console.log("The second country is " + countries[1] + "!");


/*This makes it convenient to use a for to loop through an array 
by using the length value attached to the array:*/

var countries, i;

countries = [ "Russia", "Finland", "Morocco", "Brazil" ];

for (i = 0; i < countries.length; i = i + 1) {
    console.log(countries[i]);
}


/*You can also use the push function attached to an 
existing array to add more values to it:*/

var countries;

countries = [];

countries.push( "Russia", "Finland", "Morocco", "Brazil" );
countries.push( "Canada" );
countries.push( "Mexico", "Japan" );

console.log(countries);
/* Arrays allow us to store large numbers of values within a 
single variable. They act like lists which allow us to group 
our data together in an ordered fashion.*/

//  Represent array [ ]

/* Arrays 
   Help us to store multiple values into a single value */

var friends = ["Nick" , "Michael" , "Ana" , "Maria"]

console.log (friends);
            (friends.lenght);
            (friends [ ]);
          
var friendNumber = 1;
console.log (friends [friendNumber]);

// start array with 0 
//for loop

//counter (var i=0) ; number of friend no more that that (i < 4); increase by 1 (i += i)
// i < 4 = true
// i > 4 = false (keep the loop)

for (var i=0; i < 4; i += i){
  console.log(friends [i]);
}


for (var i=0; i < friends.length; i += i){
  console.log(friends [i]);
}

---------



// Creating Arrays
// An array allows us to store a list of ordered values into a single value. An array allows  us to store a list of ordered values into a single value. Arrays represent lists of objects.


var x = ['some' , 'words' , 'here']
console.log (x.length);//3
            
// mix values
var y = ['a string' , 3, true, 'here', function(a,b){return a + b;}];
console.log (y);
console.log (y.length);//5


// sub array
var y = ['a string' , 3, ['a sub array', 2, 3, 4], 'here', function(a,b){return a + b;}]
console.log (y);
console.log (y.length);//5

//empty array
var z = [];
console.log (z.length);//0


// new array(length) - creates a new array of a given length, filled with undefined.
var a = new Array();
console.log (a);// empty

var a = new Array(50);
console.log (a);// 50



--------------



// Get and set values into an array
// Arrays allow us to retrieve and set values at specific points in our list.

var my_array = ["Hello" , 3, true, function(a){return a * 2;}];
console.log (my_array);

// Array Index
// How far from the beginning of the array an element is. Always start with 0

var word = my_array[0];
console.log (word);// Hello

var word = my_array[0];
var answer = my_array[1];
var doubler = my_array[3];
console.log (doubler(10));// 20

// Replace value 1
my array[1] = 144;
var new_answer = my_array[1];

// Assign index that doesn't exist- add to the end of the array
my array[4] = "A new string";

// my array length
my array[my_array.length] = "A new string";



---------------


// Array Methods 
// There are many methods that allow us to manipulate and inspect the contents of arrays.


// Push : pushing the value into the end of our array

var my_array = [2, 3, 4];
console.log (my_array.toString());
// my_array.toString () - returns a string representation of the array

my_array.push(5); // my_array.push(val) - appends a value to the end of the array
console.log (my_array.toString());


//Pop : return the values

var my_array = [2, 3, 4];
console.log (my_array.toString());


var value = my_array.pop(); // my_array.pop(val) - removes and returns the last value in the array
console.log (my_array.toString());


var value2 = my_array.pop(); // continues to remove numbers

2, 3, 4
2, 3, 4, 5
2, 3, 4
2, 3

value  = 5 // removed
value2 = 4 //removed


// Unshift - shift remove from the beginning of the list no the final as push and pop.


var my_array = [2, 3, 4];
console.log (my_array.toString());

// Unshift - work like push
my_array.unshift(1); // my_array.unshift(val) - Prepends a value to the beginning of the array
console.log (my_array.toString());


//shift - works like pop
var value = my_array.shift(); // my_array.shift(val) - removes and returns the first value in the array
console.log (my_array.toString());


-------------

// Order and sort arrays
// There are many methods that allow us to manipulate and inspect the contents of arrays. This video goes over a set of functions that order and sort arrays.


var my_array = [10, 44, 32, 100, 0, 44, 3, 4];
console.log (my_array.toString());

// call sort as string is default
my_array.sort();
console.log (my_array.toString());// 0, 10, 100, 3, 32, 4, 44, 44 

// myarray.sort(compareFunction)
// Sorts an array by using compareFunction to determine the order


// function takes arguments and returns values - passing annonymous function - store function into array
// function take two parameters a - b from the array - and need to return a number
// The sign of that number is what's going to determine whether a is larger than b, smaller than b, or equal to b.
// So if we return a negative number, that means a should come before b in our array.
// If we return a positive number, a should come after b in our array.
// And if we return 0, a and b are considered equal and can come in either order.
// So if we want to implement a numeric sort, we can think of it this way.
// If we want our smaller numbers to come first, then let's think about the case where a is 5 and b is 10.
// If we subtract b from a, meaning if we subtract 10 from 5, we get -5,
// which is negative and would indicate that a comes before b, which is the case.
// If a was 10 and b was 5 and we subtracted b from a, then we get positive 5,
// meaning that a should come after b which, again, is the case.
// So subtracting numbers offers a great way for us to get the correctly signed return value.
// Of course if a and b are equal, a minus b is 0, meaning they are the same, which is also what we want.
// We can only do this if we're sure that all of our elements are numbers because that's the only thing we can subtract on.
// But since we see my_array has all the correct values, we can write a very simple function saying "return a - b."
// So now JavaScript knows how to compare the 2 elements in the array and should now compare them as though they were numbers.

my_array.sort(function(a,b){
	return a - b;
});
console.log (my_array.toString()); // 0, 3, 4, 10, 32, 44, 44, 100 



my_array.sort(function(a,b){
	return Math.random() - 0.5; // random numbers
});
console.log (my_array.toString());



// sort reverse
var my_array = [1, 2, 3, 4, 5];
console.log (my_array.toString());

my_array.reverse();


// array with sort and length
 var saying1 = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"];
      var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"];
      saying1.reverse();
      saying2.sort(function(a,b){
	    return a.length - b.length; ;// ASC -> a - b; DESC -> b - a
});


--------------

// concat  -  Join two arrays 
// myArray.concat(val1, val2, val3..) - Appends all the values to the array in order

var x = [1, 2, 3];
var y = [4, 5, 6];
var z = x.concat(y); // create new array start with x finish with y
// or var z = x.concat([4, 5]); or x.concat(y, "dog");

console.log (x);
console.log (y);
console.log (z);


-----------



// slice - take a piece of the array
// myArray.slice(start, end) - returns a new array based on the array from index start to index end

var my_array = [0, 1, 2, 3, 4, 5];

var result = my_array.slice(1, 4);


console.log (result);// 1, 2, 3



-----------

// Join - better with strings
// myArray.join(separator) - returns a string with all values in array joined with the string separator

var words = ["these", "are", "some", "words"];

var result = words.join(' ');// (' ') space - ('-') dashes - ('hola') hola between words


console.log (result);// these are words


----------

// Splice - powerfull array
// The splice method is the most powerful method for manipulating arrays. It can perform just about any transformation, but it can be difficult to understand at first. 


// delete method but is better to do this with splice
var my_array = [0, 1, 2, 3, 4, 5, 6];
console.log (my_array.toString()); // 0, 1, 2, 3, 4, 5, 6

delete my_array [2]// index 2
console.log (my_array.toString()); // 0, 1,, 3, 4, 5, 6




var my_array = [0, 1, 2, 3, 4, 5, 6];

my_array.splice(2, 1) // remove 2 - 1 is deleting 1 index from the array
// my_array.splice [2, 2] // remove 2 - second 2 is deleting 2 index from the array from left to right


console.log (my_array.toString()); // 0, 1,, 3, 4, 5, 6


// insert two on array
var my_array = [0, 1, 2, 3, 4, 5, 6];

my_array.splice(2, 0, 'two') // add two in the 2 index


// change 2 with two
my_array[2] = 'two';