Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Wednesday, November 2, 2016

For Loops and For In loops

a for loop lets you iterate through and array or object.

example:

var array = [1, 2, 3, 4];

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

// this console.logs the placement/key/prop of i in the array 0, 1, 2, 3 not the value of 1, 2, 3, 4;

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

//this console.logs the value of each key so this array console.logs 1, 2, 3, 4;

the special for in loop is only for arrays;

var obj = { name: 'josh', age: '34', haircolor: 'black', address: {street: '1012 old mountain rd', city: 'Denver', country: 'USA'} } for (var prop in obj) { console.log(prop) }

//this console.logs the keys of the object asking for the name of the key
name
age
haircolor
address



for (var prop in obj) { console.log(obj[prop]) }
//this console.logs the values of the object asking for the object's property.
josh
34
black
Object {street: "1012 old mountain rd", city: "Denver", country: "USA"}

for (var prop in obj) {
  console.log(obj[prop].city)
  //  this iterates through the object values then looks for the value of a key named city
}



Tuesday, November 1, 2016

Arrays join() turns into string - concat() merges two arrays together - indexOf() find the key index of

The array join method is useful when you want to join the contents in an array in a single string.  Take the following array:

var weekdays = ["mon", "tues", "w", "thu", "fri"];

the .join() method would do the following.

var workDays = weekdays.join(", ");

This joins the array together and adds a ",  " between each value in the array.

concat() is another useful tool in javascript arrays.

concat() join's to arrays together to create one array.  see the following:

var weekends = ["sat", "sun"];

var allDays = weekdays.concat(weekends);

This creates a var allDays = ["mon", "tues", "w", "thu", "fri", "sat", "sun"].

indexOf() works well to find out the position "index" of a given value.

var sat = allDays.indexOf("sat");

This would return 5.

var frat = allDays.indexOf("frat");
This would return -1 because there isn't a fraturday in the list.

Array pop() shift()

if you wanted to remove the last item in an array you would want to use pop() method. This removes the last item from an array but also returns it.

example:

var array = [1, 2, 3];
var last = array.pop([2]);

This removes the last item from the array "3" then save's 3 in the var last.  If you wanted to use it later.

Similarly the shift() method does the same for the beginning of an array.

Arrays Unshift() and Push()

Arrays are one of the most useful ways to store data.  A great way to add data to the beginning of an array is to use the unshift() method.

Lets take a already declared array.

var array = [1, corey, true] ;

Adding to the beginning of the array is as easy as...

array.unshift("danny", false, 15);

This adds "danny", false and 15 to the array in places [0], [1], [2] in the array and moves 1, "corey" and true to places [3], [4], [5] in the array.

If you wanted to add something to end of the array you would use the push() method..

array.push("hello", 99);

This would add "hello" and 99 to the end of the array in places [6] and [7].

Hope this helps.