JS Masyvai PDF
Document Details
Uploaded by MagnanimousCloisonnism
Vilnius University
Justina Balsė
Tags
Summary
This document is a set of notes on Javascript arrays, covering topics like array methods, working with arrays, and examples. It includes explanations, examples, and code snippets.
Full Transcript
Programavimo pagrindai (Arrays) Justina Balsė Turinys Masyvai Masyvo ilgis Masyvo metodai 2 Arrays let arr = []; An array is a data structure that contains a group of elements. Justina Balsė | 3 A...
Programavimo pagrindai (Arrays) Justina Balsė Turinys Masyvai Masyvo ilgis Masyvo metodai 2 Arrays let arr = []; An array is a data structure that contains a group of elements. Justina Balsė | 3 Arrays let fruits = ["Apple", "Orange", Array elements are numbered, "Plum"]; starting with zero. We can get an element by its alert( fruits ); // Apple alert( fruits ); // Orange number in square brackets. alert( fruits ); // Plum Justina Balsė | 4 Kodėl? Justina Balsė | 5 Arrays | Example let array = []; array = 1; array = 22; array = array + array; // 1? array = array * 2; // 2? let n = array.length; console.log(array[n-1]); // 3? Justina Balsė | 6 Arrays | length let fruits = ["Apple", "Banana"]; let numbers = [11, 3.33, -65]; The total count of let mix = [1, "Apple", 22, the elements in the "Banana"]; array is its length. // Banana console.log(fruits); // 3 console.log(numbers.length); // Apple console.log(mix[mix.length-3]); Justina Balsė | 7 Arrays | toString(), join() toString()– method returns a string representing the specified array and its elements. join() – method creates and returns a new string by concatenating all of the elements in an array, separated by commas or a specified separator string. Justina Balsė | 8 Arrays | toString(), join() let fruits = ["Apple", "Banana", "Pear"]; // "Apple,Banana,Pear" console.log(fruits.toString()); // "Apple *** Banana *** Pear" console.log(fruits.join(" *** ")); Justina Balsė | 9 Methods: unshift(), shift(), pop(), push() Justina Balsė | 10 Arrays | pop(), push() pop() – method removes the last element from an array and returns that element. This method changes the length of the array. push() – method adds one or more elements to the end of an array and returns the new length of the array. Justina Balsė | 11 Arrays | pop(), push() let fruits = ["Apple", "Banana", "Pear"]; // "Pear" console.log(fruits.pop()); // "Apple,Banana" console.log(fruits.toString()); // 3 console.log(fruits.push("Pineapple")); // "Apple,Banana,Pineapple" console.log(fruits.toString()); Justina Balsė | 12 Arrays | shift(), unshift() shift()– method removes the first element from an array and returns that removed element. This method changes the length of the array. unshift()– method adds one or more elements to the beginning of an array and returns the new length of the array. Justina Balsė | 13 Arrays | shift(), unshift() let num = [1, 2, 3]; // 1 console.log(num.shift()); // [2, 3] console.log(num); // 4 console.log(num.unshift(22, 33)); // [22, 33, 2, 3] console.log(num); Justina Balsė | 14 Arrays | concat() concat() – method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. Justina Balsė | 15 Arrays | concat() let num1 = [1, 2, 3], num2 = [4, 5, 6], num3 = [7, 8, 9]; let nums = num1.concat(num2, num3); // [1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(nums); Justina Balsė | 16 Print each element of the array let array = ["abc", true, 1, 99, false]; for(let i = 0; i item * 2); Justina Balsė | 22 Arrays | filter() filter() – method creates a new array with all elements that pass the test implemented by the provided function. Callback: – element – index (optional) – array (optional) Justina Balsė | 23 Arrays | filter() let words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result1 = words.filter(function(word){ return word.length > 6; }) const result2 = words.filter(word => word.length > 6); Justina Balsė | 24 Kiti metodai find() every() some() includes() Justina Balsė | 25 Justina Balsė | 26 Arrays Copy | 1 27 Arrays Copy | 2 let array = ["abc", true, 1, 99, false]; let arrayCopy1 = []; let arrayCopy2 = []; // bad for(let i = 0; i < array.length; i++){ arrayCopy1[i] = array[i]; } // good let arrayCopy2 = [...array]; Justina Balsė | 28 Arrays Copy | 3 const sheeps = ['🐑', '🐑', '🐑']; Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the const fakeSheeps = sheeps; reference to the original array and not the value of const cloneSheeps = [...sheeps]; the array. To create a real copy of an array, you need console.log(sheeps === fakeSheeps); to copy over the value of the array under a new value // true --> it's pointing to the same memory space variable. console.log(sheeps === cloneSheeps); // false --> it's pointing to a new memory space Justina Balsė | 29 Bloga / Gera praktika const arr = [1, 2, 3, 4]; // bad const first = arr; const second = arr; // good const [first, second] = arr; Justina Balsė | 30 Bloga / Gera praktika // bad [1, 2, 3].map(function (x) { const y = x + 1; return x * y; }); // good [1, 2, 3].map((x) => { const y = x + 1; return x * y; }); Justina Balsė | 31 *Rest parameters function sum(...theArgs) { The rest parameter syntax allows a let sum = 0; for(let i=0; i