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.

No comments:

Post a Comment