Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ESTUTURA DE DADOS com JavaScript
- // ******{ VETORES/ARRAYS }********
- //JavaScript arrays are objects
- // create Array
- var numbers = [];
- // you have an array with length of 0
- console.log(numbers.length);
- // set values
- var numbers = [1,2,3,4,5];
- print(numbers.length); // displays 5
- // Other form to create an array
- var numbers = new Array();
- console.log(numbers.length); // displays 0
- // setting elements
- var numbers = new Array(1,2,3,4,5);
- console.log(numbers.length); // displays 5
- // create an array constructor with single argument specifying the length of the array
- var numbers = new Array(10);
- console.log(numbers.length); // displays 10
- // JavaScript array elements do not all have to be of the same type
- var objects = [1, "Joe", true, null];
- // verify if object is an array with Array.isArray() function
- var numbers = 3;
- var arr = [7,4,1776];
- console.log(Array.isArray(numbers)); // displays false
- console.log(Array.isArray(arr)); // displays true
- // PS: the most JavaScript experts recommend using the [] operator, saying it is more efficient than Array constructor
- //Acessing and writing Array elements
- // Data is assigned to array elements using the [] operator in an assignment statement.
- // For example, the following loop assigns the values 1 through 100 to an array:
- var nums = [];
- for (var i = 0; i < 10; ++i) {
- nums[i] = i+1;
- }
- //Array elements are also accessed using the [] operator. For example:
- var numbers = [1,2,3,4,5];
- var sum = numbers[0] + numbers[1] + numbers[2] + numbers[3] +
- numbers[4];
- console.log(sum);
- //accessing all the elements of an array sequentially is much easier using a for loop
- var numbers = [1,2,3,5,8,13,21];
- var sum = 0;
- for (var i = 0; i < numbers.length; ++i) {
- sum += numbers[i];
- }
- console.log(sum); // displays 53
- // Creating Arrays from Strings
- // split()
- //This function breaks up a string at a common delimiter, such as a space for each word, and
- //creates an array consisting of the individual parts of the string
- var sentence = "the quick brown fox jumped over the lazy dog";
- var words = sentence.split(" ");
- for (var i = 0; i < words.length; ++i) {
- console.log("word " + i + ": " + words[i]);
- }
- // Aggregate Array Operations
- //There are several aggregate operations you can perform on arrays
- // First, you can assign one array to another array
- var nums = [];
- var samenums = []
- for (var i = 0; i < 10; ++i) {
- nums[i] = i+1;
- }
- var samenums = nums;
- // However, when you assign one array to another array, you are assigning a reference to the assigned array
- // When you make a change to the original array, that change is reflected in the other array as well
- var nums = [];
- for (var i = 0; i < 100; ++i) {
- nums[i] = i+1;
- }
- var samenums = nums;
- nums[0] = 400;
- console.log(samenums[0]); // This is called a shallow copy.
- // The new array simply points to the original array’s elements.
- // (o novo array aponta para o array original)
- // A better alternative is to make a deep copy, so that each of the original array’s elements
- // is actually copied to the new array’s elements
- function copy(arr1, arr2) {
- for (var i = 0; i < arr1.length; ++i) {
- arr2[i] = arr1[i];
- }
- }
- var nums = [];
- for (var i = 0; i < 10; ++i) {
- nums[i] = i+1;
- }
- var samenums = [];
- copy(nums, samenums);
- nums[0] = 400;
- console.log(samenums[0]);// displays 1
- var nums = [1,2,3,4,5];
- console.log(nums);// 1, 2 , 3 ,4 5
- //Accessor Functions
- //functions you can use to access the elements of an array
- //Searching for a Value
- // *indexOf()
- //the function returns the index position of the argument
- //If the argument is not found in the array, the function returns -1
- var names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"];
- var name = 'Jesica';
- var position = names.indexOf(name);
- if (position >= 0) {
- console.log("Found " + name + " at position " + position);
- }
- else {
- console.log(name + " not found in array." + position);
- }
- //PS: If you have multiple occurrences of the same data in an array, the indexOf() function
- //will always return the position of the first occurrence
- // * lastIndexOf()
- //will return the position of the last occurrence of the argument in the array, or -1
- //if the argument isn’t found
- var names = ["David", "Mike", "Cynthia", "Raymond", "Clayton", "Mike","Jennifer"];
- var name = "Mike";
- var firstPos = names.indexOf(name);
- console.log("First found " + name + " at position " + firstPos); // First found Mike at position 1
- var lastPos = names.lastIndexOf(name);
- console.log("Last found " + name + " at position " + lastPos); // Last found Mike at position 5
- //String Representations of Arrays
- // * join() and toString()
- //Both functions return a string containing the elements of the array de‐
- //limited by commas.
- var names = ["David", "Cynthia", "Raymond", "Clayton", "Mike", "Jennifer"];
- var namestr = names.join();
- console.log(namestr); // David,Cynthia,Raymond,Clayton,Mike,Jennifer
- namestr = names.toString();
- console.log(namestr); // David,Cynthia,Raymond,Clayton,Mike,Jennifer
- //Creating New Arrays from Existing Arrays
- //There are two accessor functions that allow you create new arrays from existing arrays
- // * concat() and splice() .
- // Concat() - The concat() function allows you to put together two or more arrays to create a new array
- // * concat()
- var cisDept = ["Mike", "Clayton", "Terrill", "Danny", "Jennifer"];
- var dmpDept = ["Raymond", "Cynthia", "Bryan"];
- var itDiv = cisDept.concat(dmpDept);
- console.log(itDiv);
- // * Splice()
- var itDiv = ["Mike","Clayton","Terrill","Raymond","Cynthia","Danny","Jennifer"];
- var dmpDept = itDiv.splice(3,3);
- var cisDept = itDiv;
- console.log(dmpDept); // Raymond,Cynthia,Danny
- console.log(cisDept); // Mike,Clayton,Terrill,Jennifer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement