Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data Types: Arrays and Objects

Data Types: Arrays and Objects

John Nunemaker

September 08, 2009
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. different ways to do the same thing var student =

    new Object(); student.name = 'John Doe'; student.age = 18; var student = {name:'John Doe', age:18}; Tuesday, September 8, 2009
  2. keys can be strings var student = {name:'John Doe', age:18};

    var student = {'name':'John Doe', 'age':18}; Tuesday, September 8, 2009
  3. liberal formatting var student = {name:'John Doe', age:18}; var student

    = { name:'John Doe', age:18 }; var student = { name : 'John Doe', age : 18 }; Tuesday, September 8, 2009
  4. write as many as you want var student = {};

    student.name = 'John Doe'; student.age = 18; student.gpa = 3.7; Tuesday, September 8, 2009
  5. reading properties var student = {name:'John Doe', age:18}; student.name //

    "John Doe" student.age // 18 Tuesday, September 8, 2009
  6. reading properties (alternative syntax) var student = {name:'John Doe', age:18};

    student['name'] // "John Doe" student['age'] // 18 Tuesday, September 8, 2009
  7. reading properties that are not set var student = {name:'John

    Doe', age:18}; student.foobar // undefined Tuesday, September 8, 2009
  8. different ways to do the same thing var student =

    {name:'John Doe', age:18}; student.name // "John Doe" student['name'] // "John Doe" student.age // 18 student['age'] // 18 Tuesday, September 8, 2009
  9. new Array with no arguments var things = new Array();

    things.length; // 0 creates empty array Tuesday, September 8, 2009
  10. new Array with one argument var things = new Array(5);

    things.length; // 5 creates array with number of undefined elements shouldn’t ever need to do this, but be aware Tuesday, September 8, 2009
  11. new Array with multiple arguments creates array with each argument

    as element in array var things = new Array(1,2,3); things.length; // 3 Tuesday, September 8, 2009
  12. remember how strings are zero indexed? var car = "car";

    car[0] // c car[1] // a car[2] // r Tuesday, September 8, 2009
  13. arrays are too var car = ['c', 'a', 'r']; car[0]

    // c car[1] // a car[2] // r Tuesday, September 8, 2009
  14. array of numbers var things = [1,2,3,4,5]; things[0] // 1

    things[1] // 2 things[2] // 3 things[3] // 4 things[4] // 5 things[5] // undefined Tuesday, September 8, 2009
  15. remember how strings know their length? var football = "football";

    football.length // 8 Tuesday, September 8, 2009
  16. arrays do too var things = [1,2,3,4,5]; things.length // 5

    http://www.w3schools.com/jsref/jsref_length_array.asp Tuesday, September 8, 2009
  17. remember how strings can get the indexOf stuff? var football

    = "football"; football.indexOf('f') // 0 football.indexOf('o') // 1 football.indexOf('foot') // 0 football.indexOf('z') // -1 Tuesday, September 8, 2009
  18. arrays can too var car = ['c','a','r']; car.indexOf('c'); // 0

    car.indexOf('a'); // 1 car.indexOf('r'); // 2 car.indexOf('b'); // -1 Tuesday, September 8, 2009
  19. more indexOf examples var strings = ['aa', 'b', 'ccD']; strings.indexOf('aa')

    // 0 strings.indexOf('ccD') // 2 strings.indexOf('ccd') // -1 Tuesday, September 8, 2009
  20. arrays can be multi-dimensional var multi = ['a', 'b', ['z',

    'y']]; multi[0] // 'a' multi[1] // 'b' multi[2] // ['z', 'y'] multi[2][0] // 'z' multi[2][1] // 'y' multi[2].length // 2 Tuesday, September 8, 2009
  21. concat of a and b var a = [1,2]; var

    b = [3,4]; var c = a.concat(b); // a is still [1,2] // b is still [3,4] // c is now [1,2,3,4] Tuesday, September 8, 2009
  22. concat of b and a var a = [1,2]; var

    b = [3,4]; var c = b.concat(a); // a is still [1,2] // b is still [3,4] // c is now [3,4,1,2] Tuesday, September 8, 2009
  23. concat with multi-dimensional var multi = ['a', 'b', ['z', 'y']];

    multi.concat([1, 2]) // ['a', 'b', ['z','y'], '1', '2'] Tuesday, September 8, 2009
  24. join joins the elements of an array into a string

    separated by a delimiter http://www.w3schools.com/jsref/jsref_join.asp Tuesday, September 8, 2009
  25. delimiter can be any string var car_letters = ['c', 'a',

    'r']; car_letters.join('WIN'); // "cWINaWINr" Tuesday, September 8, 2009
  26. date example var date = [9, 8, 2009] date.join('/'); //

    "9/8/2009" Tuesday, September 8, 2009
  27. string has the opposite of join called split var date

    = "9/8/2009" date.split('/') // [‘9’,‘8’,‘2009’] Tuesday, September 8, 2009
  28. split/join allow for switching back and forth var date =

    "9/8/2009"; var date_array = date.split('/') // [‘9’,‘8’,‘2009’] var date_string = date_array.join('/'); // “9/8/2009” Tuesday, September 8, 2009
  29. reverse var car = ['r', 'a', 'c']; car.reverse(); // ['c',

    'a', 'r'] Tuesday, September 8, 2009
  30. sorting strings var names = ['John', 'Steph', 'Steve', 'Carrie']; names.sort();

    // ['Carrie','John','Steph','Steve'] Tuesday, September 8, 2009
  31. upper case before lower case var letters = ['a', 'B',

    'c', 'D']; letters.sort(); // ['B','D','a','c'] Tuesday, September 8, 2009
  32. pop removes and returns the last element of an array

    (opposite of shift) http://www.w3schools.com/jsref/jsref_pop.asp Tuesday, September 8, 2009
  33. pop var car = ['c', 'a', 'r']; var last =

    car.pop(); last // 'r' car // ['c','a'] Tuesday, September 8, 2009
  34. pop var car = ['c', 'a', 'r']; car.pop(); // 'r'

    car.pop(); // 'a' car.pop(); // 'c' car.pop(); // undefined car.length // 0 Tuesday, September 8, 2009
  35. shift removes and returns the first element of an array

    (opposite of pop) http://www.w3schools.com/jsref/jsref_shift.asp Tuesday, September 8, 2009
  36. shift var car = ['c', 'a', 'r']; var first =

    car.shift(); first // 'c' car // ['a','r'] Tuesday, September 8, 2009
  37. shift var car = ['c', 'a', 'r']; car.shift(); // 'c'

    car.shift(); // 'a' car.shift(); // 'r' car.shift(); // undefined car.length // 0 Tuesday, September 8, 2009
  38. push adds one or more elements to the end of

    an array and returns the new length (opposite of unshift) http://www.w3schools.com/jsref/jsref_push.asp Tuesday, September 8, 2009
  39. push single element var car = ['c','a','r']; car.push('d'); // 4

    car // ['c','a','r','d'] Tuesday, September 8, 2009
  40. push multiple elements var car = ['c','a','r']; car.push('d', 'e', 'd');

    // 6 car // ['c','a','r','d', 'e', 'd'] Tuesday, September 8, 2009
  41. unshift adds one or more elements to the beginning of

    the array and returns the new length (opposite of push) http://www.w3schools.com/jsref/jsref_unshift.asp Tuesday, September 8, 2009
  42. unshift single element var car = ['c','a','r']; car.unshift('s'); // 4

    car // ['s','c','a','r'] Tuesday, September 8, 2009
  43. unshift multiple elements var car = ['c','a','r']; car.unshift('o', 's'); //

    5 car // ['o','s','c','a','r'] Tuesday, September 8, 2009
  44. slice with one argument var numbers = [1,2,3,4]; numbers.slice(2); //

    [3, 4] numbers // [1,2,3,4] slices from index to end of array Tuesday, September 8, 2009
  45. slice with two arguments slices from beginning index to index

    before second argument var numbers = [1,2,3,4]; numbers.slice(0, 2); // [1, 2] numbers // [1,2,3,4] Tuesday, September 8, 2009
  46. slice with negative second argument var numbers = [1,2,3,4]; numbers.slice(1,

    -1); // [2, 3] numbers.slice(1, -2); // [2] numbers // [1,2,3,4] negative starts from end and goes backwards Tuesday, September 8, 2009
  47. splice parameters array.splice(index, how many to remove, elements to add)

    index and how many to remove are required Tuesday, September 8, 2009
  48. removing an element from an array var letters = ['a',

    'c', 'd']; letters.splice(1, 1); // 'c' letters // ['a','d'] in english: at index 1, remove 1 element Tuesday, September 8, 2009
  49. removing multiple elements from an array var letters = ['a',

    'c', 'd']; letters.splice(1, 2); // ['c','d'] letters // ['a'] in english: at index 1, remove 2 elements Tuesday, September 8, 2009
  50. adding an element to an array var letters = ['a',

    'c', 'd']; letters.splice(1, 0, 'b'); // ['a', 'b', 'c', 'd'] in english: at index 1, remove 0 elements, add ‘b’ Tuesday, September 8, 2009
  51. adding multiple elements to an array in english: at index

    1, remove 0 elements, add ‘b’ and ‘c’ var letters = ['a', 'c', 'd']; letters.splice(1, 0, 'b', 'c'); letters // ['a','b','c','c','d'] Tuesday, September 8, 2009
  52. replacing elements in an array var car = ['c', 'a',

    'r']; car.splice(0, 1, 'b'); // 'c' car // ['b','a','r'] Tuesday, September 8, 2009