Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. // ESTUTURA DE DADOS com JavaScript
  2.  
  3. // ******{ VETORES/ARRAYS }********
  4.  
  5. //JavaScript arrays are objects
  6.  
  7. // create Array
  8. var numbers = [];
  9.  
  10. // you have an array with length of 0
  11. console.log(numbers.length);
  12.  
  13. // set values
  14. var numbers = [1,2,3,4,5];
  15. print(numbers.length); // displays 5
  16.  
  17. // Other form to create an array
  18. var numbers = new Array();
  19. console.log(numbers.length); // displays 0
  20.  
  21. // setting elements
  22. var numbers = new Array(1,2,3,4,5);
  23. console.log(numbers.length); // displays 5
  24.  
  25. // create an array constructor with single argument specifying the length of the array
  26. var numbers = new Array(10);
  27. console.log(numbers.length); // displays 10
  28.  
  29. // JavaScript array elements do not all have to be of the same type
  30. var objects = [1, "Joe", true, null];
  31.  
  32. // verify if object is an array with Array.isArray() function
  33. var numbers = 3;
  34. var arr = [7,4,1776];
  35. console.log(Array.isArray(numbers)); // displays false
  36. console.log(Array.isArray(arr)); // displays true
  37.  
  38. // PS: the most JavaScript experts recommend using the [] operator, saying it is more efficient than Array constructor
  39.  
  40. //Acessing and writing Array elements
  41.  
  42. // Data is assigned to array elements using the [] operator in an assignment statement.
  43. // For example, the following loop assigns the values 1 through 100 to an array:
  44. var nums = [];
  45. for (var i = 0; i < 10; ++i) {
  46. nums[i] = i+1;
  47. }
  48.  
  49. //Array elements are also accessed using the [] operator. For example:
  50. var numbers = [1,2,3,4,5];
  51. var sum = numbers[0] + numbers[1] + numbers[2] + numbers[3] +
  52. numbers[4];
  53. console.log(sum);
  54.  
  55. //accessing all the elements of an array sequentially is much easier using a for loop
  56. var numbers = [1,2,3,5,8,13,21];
  57. var sum = 0;
  58. for (var i = 0; i < numbers.length; ++i) {
  59. sum += numbers[i];
  60. }
  61.  
  62. console.log(sum); // displays 53
  63.  
  64. // Creating Arrays from Strings
  65. // split()
  66. //This function breaks up a string at a common delimiter, such as a space for each word, and
  67. //creates an array consisting of the individual parts of the string
  68.  
  69. var sentence = "the quick brown fox jumped over the lazy dog";
  70. var words = sentence.split(" ");
  71. for (var i = 0; i < words.length; ++i) {
  72. console.log("word " + i + ": " + words[i]);
  73. }
  74.  
  75. // Aggregate Array Operations
  76. //There are several aggregate operations you can perform on arrays
  77. // First, you can assign one array to another array
  78. var nums = [];
  79. var samenums = []
  80. for (var i = 0; i < 10; ++i) {
  81. nums[i] = i+1;
  82. }
  83. var samenums = nums;
  84.  
  85. // However, when you assign one array to another array, you are assigning a reference to the assigned array
  86. // When you make a change to the original array, that change is reflected in the other array as well
  87. var nums = [];
  88. for (var i = 0; i < 100; ++i) {
  89. nums[i] = i+1;
  90. }
  91.  
  92. var samenums = nums;
  93. nums[0] = 400;
  94. console.log(samenums[0]); // This is called a shallow copy.
  95. // The new array simply points to the original array’s elements.
  96. // (o novo array aponta para o array original)
  97.  
  98. // A better alternative is to make a deep copy, so that each of the original array’s elements
  99. // is actually copied to the new array’s elements
  100.  
  101. function copy(arr1, arr2) {
  102. for (var i = 0; i < arr1.length; ++i) {
  103. arr2[i] = arr1[i];
  104. }
  105. }
  106.  
  107. var nums = [];
  108. for (var i = 0; i < 10; ++i) {
  109. nums[i] = i+1;
  110. }
  111. var samenums = [];
  112.  
  113. copy(nums, samenums);
  114. nums[0] = 400;
  115. console.log(samenums[0]);// displays 1
  116. var nums = [1,2,3,4,5];
  117. console.log(nums);// 1, 2 , 3 ,4 5
  118.  
  119. //Accessor Functions
  120. //functions you can use to access the elements of an array
  121.  
  122. //Searching for a Value
  123. // *indexOf()
  124. //the function returns the index position of the argument
  125. //If the argument is not found in the array, the function returns -1
  126. var names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"];
  127. var name = 'Jesica';
  128. var position = names.indexOf(name);
  129. if (position >= 0) {
  130. console.log("Found " + name + " at position " + position);
  131. }
  132. else {
  133. console.log(name + " not found in array." + position);
  134. }
  135.  
  136. //PS: If you have multiple occurrences of the same data in an array, the indexOf() function
  137. //will always return the position of the first occurrence
  138.  
  139. // * lastIndexOf()
  140. //will return the position of the last occurrence of the argument in the array, or -1
  141. //if the argument isn’t found
  142.  
  143. var names = ["David", "Mike", "Cynthia", "Raymond", "Clayton", "Mike","Jennifer"];
  144. var name = "Mike";
  145. var firstPos = names.indexOf(name);
  146.  
  147. console.log("First found " + name + " at position " + firstPos); // First found Mike at position 1
  148. var lastPos = names.lastIndexOf(name);
  149. console.log("Last found " + name + " at position " + lastPos); // Last found Mike at position 5
  150.  
  151. //String Representations of Arrays
  152. // * join() and toString()
  153.  
  154. //Both functions return a string containing the elements of the array de‐
  155. //limited by commas.
  156. var names = ["David", "Cynthia", "Raymond", "Clayton", "Mike", "Jennifer"];
  157. var namestr = names.join();
  158. console.log(namestr); // David,Cynthia,Raymond,Clayton,Mike,Jennifer
  159. namestr = names.toString();
  160. console.log(namestr); // David,Cynthia,Raymond,Clayton,Mike,Jennifer
  161.  
  162. //Creating New Arrays from Existing Arrays
  163. //There are two accessor functions that allow you create new arrays from existing arrays
  164. // * concat() and splice() .
  165. // Concat() - The concat() function allows you to put together two or more arrays to create a new array
  166.  
  167. // * concat()
  168. var cisDept = ["Mike", "Clayton", "Terrill", "Danny", "Jennifer"];
  169. var dmpDept = ["Raymond", "Cynthia", "Bryan"];
  170. var itDiv = cisDept.concat(dmpDept);
  171. console.log(itDiv);
  172.  
  173. // * Splice()
  174. var itDiv = ["Mike","Clayton","Terrill","Raymond","Cynthia","Danny","Jennifer"];
  175. var dmpDept = itDiv.splice(3,3);
  176. var cisDept = itDiv;
  177. console.log(dmpDept); // Raymond,Cynthia,Danny
  178. console.log(cisDept); // Mike,Clayton,Terrill,Jennifer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement