TheIronYard--Orlando / FEE--2014--FALL

The Handbook for the Front End Engineering class of the Fall 2014 cohort at The Iron Yard, Orlando Campus
Creative Commons Zero v1.0 Universal
14 stars 13 forks source link

TIY-Assignments / Dojos / 11--Arrays.ProtoypeList-Nova.md #203

Closed Xx-Nova-xX closed 10 years ago

Xx-Nova-xX commented 10 years ago

* .length = arr.length

arr.length - tells you how many elements are in an array

var letters = [ ‘a’, ‘b’, ‘c’, ‘d’]; --> letters.length = 4

* .copyWithin = arr.copyWithin(target, start[, end = this.length])

arr.copyWithin – copies the sequence of array elements in the array.

 

[1, 2, 3, 4, 5].copywithin(0, 3); --> [4,5,3,4,5]

 

*.fill = arr.fill(value[, start = 0[, end = this.length]])

 

arr.fill – fills all array elements from start to end with a static value

 

[1, 2, 3].fill(4) --> [4, 4, 4]

 

 

*.pop = arr.pop()

arr.pop – removes the last element of the array and returns that element

 

var myCars = [‘Toyota’, ‘Lexus’, ‘Lambo’, ‘Bugatti’];

var popped = myCars.pop();

console.log(myCars); --> [‘Toyota’, ‘Lexus’, ‘Lambo’,]

console.log(popped); --> ‘Bugatti'

 

 

*.push = arr.push(element1, …, element)

arr.push – adds one or more elements to the end of an array and returns the new length of the array

 

var sports = [‘basketball’, ‘hokey’];

var total = sports.push(‘football’, ‘swimming’);

console.log(sports): --> ‘basketball’, ‘hockey’, ‘football’, ‘swimming’]

console.log(total); --> 4

*.reverse = arr.reverse()

arr.reverse - takes an array and lists it backwards.

 

Var myArray = [‘one’, ‘two’, ‘three];

myArray.reverse();

console.log(myArray) --> ‘three’, ‘two’, ‘one’

 

 

*.shift() = arr.shift()

arr.shift – removes the [0] index spot and shifts the values.

 

var myFish = [‘angel’, ‘clown’, ‘mandarin’, ‘betta’];

console.log(‘myFish before: ‘ + myFish);

var shifted = myFish.shift();

console.log(‘myFish after: ‘ + myFish); --> clown, mandarin, betta

console.log(‘Removed this element: ‘ + shifted); --> angel

 

 

*sort() = arr.sort)[compareFunction])

arr.sort – sorts an array’s elements according to string Unicode code points. Can sort Objects by feeding it the value of one of their properties.

 

 

Ex.1: Var fruit[‘apples’, ‘bananas’, ‘Cherries’];

fruit.sort();--> [‘Cherries’, ‘apples’, ‘bananas’];

 

 

*.splice() = array.splice(index, howMany[, element1[, …[, elementN]]])

splice() changes array elements by adding new ones & removing old

 

it starts at the length of the array

remove.

simply removes elements from the array

 

var myFish = [ 'angel', 'clown', 'mandarin', 'surgeon' ];

var removed = myFish.splice(2, 0, 'drum'); à removes 0 elements from index 2

and inserts 'drum'

à myFish is [ 'angel', 'clown', 'drum', 'mandarin', 'surgeon' ]

 

*.unshift() = arr.unshift([element1[, …[, elementN]]])

unshift() method adds one or more elements to the start of an array & returns

new array length

 

elementN: elements to add to the start of the array

 

var arr = [ 1, 2];

arr.unshift(0); --> arr is [0, 1, 2] //result of call is 3, the new array length

 

arr.unshift(-2, -1); --> = 5 --> arr is [-2, -1, 0, 1, 2]

 

 

*.concat() = var new_array = old_arry.concat(value1[, value2[, …[, valueN]]])

concat() returns a new array made up of the original array to join the two.

 

valueN: arrays and/or values to concatenate into a new array

 

var alpha = [ 'a', 'b', 'c'],

numeric = [ 1, 2, 3 ];

 

var alphaNumeric = alpha.concat(numeric);

console.log(alphaNumeric); --> [ 'a', 'b', 'c', 1, 2, 3 ]

 

 

*.contains() = array.contains(searchElement[, fromIndex])

.contains: looks for an element in an array & return a true or false value

 

 

[1, 2, 3].contains(2); --> true } [1,2,3].contains(3, 3); --> false

[1, 2, 3].contains(4); --> false } [1,2,3].contains(3, -1); -->true

 

 

*.join() = str = arr.join([separator = ','])

join() calls all elements of an array into a string

 

var a = new Array('Wind, 'Rain', 'Fire');

var myVar1 = a.join();--> assigns 'Wind,Rain,Fire'

var myVar2 = a.join(',') --> assigns 'Wind, Rain, Fire'

var myVar3 = a.join(' + ') --> assigns 'Wind + Rain + Fire'

 

 

*.slice() = arr.slice([begin[, end]])

.slice() method removes elements in an array and holds them in a variable

 

var fruits = [ 'Banana', 'Orange', 'Lemon', 'Apple', 'Mango' ];

var citrus = fruits.slice(1, 3); --> citrus = [ 'Orange', 'Lemon' ]

 

*.toSource() = arr.toSource

.toSource returns a string representation of the source code of the array

 

var alpha = new Array ( 'a', 'b', 'c' );

alpha.toSource();--> returns [ 'a', 'b', 'c' ]

 

 

*.toString() = arr.toString()

.toString returns a string that represents the elements of the specified array

 

var monthNames = [ 'Jan', 'Feb', 'Mar', 'Apr'];

var myVar = monthNames.toString();--> assigns 'Jan,Feb,Mar,Apr' to myVar

 

*.toLocaleString() = arr.toLocaleString()

.toLocalString returns a string that represents the elements of the array

 

var number = 1337;

var date = new Date();

var myArr = [number, date, 'foo'];

var str = myArr.toLocaleString();

console.log(str); à '1.337,6.12.2013 19:37:35,foo' //in a German (de-DE) locale

with timezone Europe/Berlin

*.indexOf() = arr.indexOf(searchElement[, fromIndex = 0])

.indexOf() returns the first index where the specified array element can be found

 

var array = [2, 5, 9];

var index = array.indexOf(2); --> index is 0

index = array.indexOf(7); --> index is -1

index = array.indexOf(9,2); --> index is 2

index = array.indexOf(2, -1) --> index is -1

 

*lastIndexOf() = arr.lastIndexOf(searchElement[, fromIndex = arr.length])

.lastIndexOf returns the last index number where the element is in the array or -1 if it isn't an element in the array.

 

var array = [2, 5, 9, 2];

var index = arr.lastIndexOf(2); --> 3

index = arr.lastIndexOf(7); --> -1

index = arr.lastIndexOf(2,3) --> 3

index = array.lastIndexOf(2,2) --> 0

 

 

*.forEach = arr.forEach(callback[, thisARG])

 

executes a function you create once per array element

function logArrayElements(element, index, array){

console.log['a]' + index + '] = ' + element);

}

[2, 5, , 9].forEach(logArrayElements);

a[0] = 2

a[1] = 5

a[3] = 9

 

 

*.entries() = arr.entries();

.entries() returns a new Array Interator object that contains the key/value pairs for each index in the array.

 

var arr = ['a', 'b', 'c'];

var eArr = arr.entries();

 

console.log(eArr.next().value; --> [0, 'a']

console.log(eArr.next().value; --> [1, 'b']

console.log(eArr.next().value; --> [2, 'c']

 

 

*.every() = arr.every(callback[, thisArg])

.every() tests to see if all elements in the array pass the test implimented by the function

 

function isBigEnough(element, index, array) {

return element >= 10;

}

var passed = [12, 5, 8, 130, 44].every(isBigEnough); --> false

passed = [12, 54, 18, 130, 44].every(isBigEnough); --> true

 

 

*.some() = arr.some(callback[, thisArg])

.some() tests to see if some array elements pass the test implimented by the function

 

function isBigEnough(element, index, array) {

return element >= 10;

}

var passed = [2, 5, 8, 1, 4].some(isBigEnough); --> false

var passed = [12, 5, 8, 1, 4].some(isBigEnough); --> true

 

 

*.filter() = arr.filter(callback[, thisArg])

.filter() creates a new array with all the elements that pass the test parameters provided by the function

 

function isBigEnough(element) {

return element >= 10;

}

var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); --> filtered is [12, 130, 44]

 

 

*.find() = arr.find(callback[, thisArg])

.find() returns an array value if the element satisfies the test function, otherwise it is undefined

 

function isPrime(element, index, array){

var start = 2;

while start = 2;

while(start <= Math.sqrt(element)) {

if (element % start++ < 1) {

return false;

}

}

return element > 1;

}

console.log([4, 6, 8, 12].find(isPrime)); --> undefined, not found

console.log([4, 5, 8, 12].find(isPrime)); --> 5

 

 

*.findindex() = arr.findIndex(callback[, thisArg])

.findindex() returns and index in the array if testing function is satisfied, otherwise it returns -1

 

function isPrime(element, index, array) {

var start = 2;

while (start <= Math.sqrt(element)) {

if (element % start++ < 1){

return false;

}

}

return element > 1;

}

console.log([4, 6, 8, 12].findIndex(isPrime)); --> -1, not found

console.log([4, 6, 7, 12].findIndex(isPrime)); --> 2

 

 

*keys() = arr.keys()

.keys() returns a new Array Interator that contains the keys for every index in the array

 

var arr = [ 'a', 'b', 'c' ];

var earr = arr.keys();

console.log(eArr.next().value); --> 0

console.log(eArr.next().value); --> 1

console.log(eArr.next().value); --> 2

 

 

*map() = arr.map(callback[, thisArg])

.map() calls a function on every element and creates a new array with the results

 

var numbers = [1, 4, 9];

var roots = numbers.map(Math.sqrt); --> roots = [1, 2, 3], numbers stays the same

 

var numbers = [1, 4, 9]; //ASCII encoding representation

var a = map.call('Hello World' , function(x) { return x.charCodeAt(0); });

à a = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

 

 

*reduce() = arr.reduce(callback[, initialValue])

.reduce() applies a function against an accumulator & each value in the array from left to right has to reduce it to a single value

 

var total = [0, 1, 2, 3].reduce(function(a, b) {

return a + b;

}); --> 6

 

var flattened = [[0,1], [2,3], [4,5]].reduce(function(a,b) {

return a.concat(b);

}); --> flattened is [0,1,2,3,4,5]

 

 

*.reduceRight() = arr.reduceRight(callback[, initalValue])

.reduceRight() applies a function against an accumulator – each array element from right to left has to reduce it to a single value

 

var total = [0,1,2,3].reduceRight(function(a,b) {

return a + b;

}); -->

Xx-Nova-xX commented 10 years ago

https://github.com/Xx-Nova-xX/TIY-Assignments/blob/master/Dojos/11--Arrays.ProtoypeList-Nova.md