Arrays Flashcards
Array
• stores a list of variables
2 ways to declare them
Var array = […, ….];
var num = new array(….,….); //uncommon way to declare.
• you can have various types within an array.
Element
• Each item inside an array
Array Index
• Position number of an element inside an array
embedded array
• An array within an array
• use “arrayName[2][0];
this example calls the index position of the embedded/inner array and the zero element within the index.
.length
- a property that tells you how many elements there are in an array.
- the last index in an array is always the same number as the length of the array minus 1
xxxx.push();
• adds element to the END of an array.
xxxx.pop()
• removes an element from the END Of an array.
xxxx.shift();
• removes an element from the beginning of an array
xxxx.unshift(“item1”, “item2);
• Adds 2 elements to the beginning of an array.
xxxx.splice(2, 2, “item1”);
- insert items and or delete items to an array.
- 1st number is index position where you want to start.
- 2nd number, number of elements to remove, starting with the first element that comes the element(s) that you’re splicing in.
xxxx.slice(2, 4);
- copy one or more consecutive elements into a new array.
- 1st digit index of 1st element to be copied.
- 2nd digit is the index of the element after the last element to be copied.
concat
• combine arrays together
• firstArray.concat(otherArray) will combine the elements in both arrays.
• To combine multiple arrays put the extra arrays inside the parentheses.
firstArray.concat(otherArray, moreArrays);
e.g.
var items = [1, 2];
var newItems = items.concat(3);
xxxx.indexOf(“element”);
- takes an element and returns the FIRST index number where it can be found.
- multiple occurrences it will list the first one.
- returns -1 if not found.
xxxx.forEach(function(element) { // statement });
- JavaScript built in method of iterating through a for loop.
- Allows you to iterate an array faster instead of using a for loop.
- array.forEach(someFunction)