logo

TypeScript masyvai

Masyvas yra vienalytė panašaus tipo elementų, turinčių gretimą atminties vietą, rinkinys.

Masyvas yra vartotojo apibrėžtas duomenų tipas.

Masyvas yra duomenų struktūros tipas, kuriame saugome panašaus tipo duomenų elementus. Masyve galime saugoti tik fiksuotą elementų rinkinį. Taip pat galime naudoti kaip objektą.

Masyvas yra indeksu pagrįsta saugykla, kurioje pirmasis elementas saugomas indeksu 0. Žemiau pateikta struktūra padeda suprasti masyvo struktūrą.

TypeScript masyvai

Masyvo charakteristikos

  1. Masyve saugomi to paties tipo duomenų elementai.
  2. Masyvo elementai, saugomi gretimose atminties vietose.
  3. 2-D masyvo elementų saugykla yra išdėstyta eilėmis gretimoje atminties vietoje.
  4. Masyvo pavadinimas reiškia pradinio elemento adresą.
  5. Masyvo dydis turėtų būti inicijuotas deklaravimo metu.
  6. Masyvo dydis turi būti pastovi išraiška, o ne kintamasis.
  7. Masyvo elementus galime gauti nurodydami atitinkamą elemento indekso reikšmę.

Privalumas

Kodo optimizavimas: Masyvas padeda optimizuoti kodą, o tai padidina programos greitį ir našumą. Tai leidžia mums efektyviau gauti arba rūšiuoti masyvo duomenis.

Atsitiktinė prieiga: Tai suteikia galimybę pasiekti bet kokius masyvo duomenis pastoviu laiku (nepriklausomai nuo jo padėties ir dydžio). Taigi mes galime gauti bet kokius masyvo, esančio bet kurioje indekso pozicijoje, duomenis tiesiogiai.

Trūkumas

Dydžio apribojimas: Masyvas leidžia saugoti tik fiksuotą elementų skaičių. Kai masyvas yra paskelbtas, jo dydžio keisti negalime. Taigi, jei norime įterpti daugiau elementų nei deklaruota, tai neįmanoma.

Masyvo deklaracija

Kaip ir „JavaScript“, „TypeScript“ taip pat palaiko masyvus. Yra du būdai deklaruoti masyvą:

1. Naudojant laužtinius skliaustus.

 let array_name[:datatype] = [val1,val2,valn..] 

Pavyzdys:

 let fruits: string[] = ['Apple', 'Orange', 'Banana']; 

2. Bendrojo masyvo tipo naudojimas.

cpp lygus
 let array_name: Array = [val1,val2,valn..] 

Pavyzdys:

 let fruits: Array = ['Apple', 'Orange', 'Banana']; 

Masyvo tipai „TypeScript“.

Yra du masyvo tipai:

  1. Vienmatis masyvas
  2. Daugiamatis masyvas
TypeScript masyvai

Vienmatis masyvas

Vienmatis masyvas yra linijinio masyvo tipas, kuriame yra tik viena eilutė duomenims saugoti. Jame yra vienas laužtinių skliaustų rinkinys („[]“). Jo elementus galime pasiekti naudodami eilutės arba stulpelio indeksą.

Sintaksė

 let array_name[:datatype]; 

Inicijavimas

 array_name = [val1,val2,valn..] 

Pavyzdys

 let arr:number[]; arr = [1, 2, 3, 4] console.log('Array[0]: ' +arr[0]); console.log('Array[1]: ' +arr[1]); 

Išvestis:

 Array[0]: 1 Array[1]: 2 

Daugiamatis masyvas

Daugiamatis masyvas yra masyvas, kuriame yra vienas ar daugiau masyvų. Kelių matmenų masyve duomenys saugomi eilučių ir stulpelių indekse (taip pat žinomas kaip matricos forma). Dvimatis masyvas (2-D masyvas) yra paprasčiausia daugiamačio masyvo forma.

TypeScript masyvai

Sintaksė

 let arr_name:datatype[][] = [ [a1,a2,a3], [b1,b2,b3] ]; 

Inicijavimas

 let arr_name:datatype[initial_array_index][referenced_array_index] = [ [val1,val2,val 3], [v1,v2,v3]]; 

Pavyzdys

 var mArray:number[][] = [[1,2,3],[5,6,7]] ; console.log(mArray[0][0]); console.log(mArray[0][1]); console.log(mArray[0][2]); console.log(); console.log(mArray[1][0]); console.log(mArray[1][1]); console.log(mArray[1][2]); 

Išvestis:

 1 2 3 5 6 7 

Masyvo objektas

Masyvo objektai leidžia mums išsaugoti kelias reikšmes viename kintamajame. Masyvą galime sukurti naudodami objektą Array. Masyvo konstruktorius naudojamas toliau nurodytiems masyvo kūrimo argumentams perduoti.

  • Skaitinė reikšmė, nurodanti masyvo dydį arba
  • Kableliais atskirtų reikšmių sąrašas.

Sintaksė

 let arr_name:datatype[] = new Array(values); 

Pavyzdys

 //array by using the Array object. let arr:string[] = new Array(&apos;JavaTpoint&apos;,&apos;2200&apos;,&apos;Java&apos;,&apos;Abhishek&apos;); for(var i = 0;i <arr.length;i++) { console.log(arr[i]); } < pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2200 Java Abhishek </pre> <h3>Array Traversal by using a for...in loop</h3> <p> <strong>Example</strong> </p> <pre> let i:any; let arr:string[] = [&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;]; for(i in arr) { console.log(arr[i]) } </pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2300 Java Abhishek </pre> <h3>Passing Arrays to Functions</h3> <p>We can pass arrays to functions by specifying the array name without an index.</p> <p> <strong>Example</strong> </p> <pre> let arr:string[] = new Array(&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;); //Passing arrays in function function display(arr_values:string[]) { for(let i = 0;i <arr_values.length;i++) { console.log(arr[i]); } calling arrays in function display(arr); < pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2300 Java Abhishek </pre> <hr> <h2>TypeScript Spread operator</h2> <p>The spread operator is used to initialize arrays and objects from another array or object. We can also use it for object de-structuring. It is a part of the ES 6 version.</p> <p> <strong>Example</strong> </p> <pre> let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log(&apos;CopiedArray: &apos; +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log(&apos;NewArray: &apos; +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log(&apos;MergedArray: &apos; +mergedArray); </pre> <p> <strong>Output:</strong> </p> <pre> CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 </pre> <hr> <h2>Array Methods</h2> <p>The list of array methods with their description is given below.</p> <table class="table"> <tr> <th>SN</th> <th>Method</th> <th>Description</th> </tr> <tr> <td>1.</td> <td>concat()</td> <td>It is used to joins two arrays and returns the combined result.</td> </tr> <tr> <td>2.</td> <td>copyWithin()</td> <td>It copies a sequence of an element within the array.</td> </tr> <tr> <td>3.</td> <td>every()</td> <td>It returns true if every element in the array satisfies the provided testing function.</td> </tr> <tr> <td>4.</td> <td>fill()</td> <td>It fills an array with a static value from the specified start to end index.</td> </tr> <tr> <td>5.</td> <td>indexOf()</td> <td>It returns the index of the matching element in the array, otherwise -1.</td> </tr> <tr> <td>6.</td> <td>includes()</td> <td>It is used to check whether the array contains a certain element or not.</td> </tr> <tr> <td>7.</td> <td>Join()</td> <td>It is used to joins all elements of an array into a string.</td> </tr> <tr> <td>8.</td> <td>lastIndexOf()</td> <td>It returns the last index of an element in the array.</td> </tr> <tr> <td>9.</td> <td>Pop()</td> <td>It is used to removes the last elements of the array.</td> </tr> <tr> <td>10.</td> <td>Push()</td> <td>It is used to add new elements to the array.</td> </tr> <tr> <td>11.</td> <td>reverse()</td> <td>It is used to reverse the order of an element in the array.</td> </tr> <tr> <td>12.</td> <td>Shift()</td> <td>It is used to removes and returns the first element of an array.</td> </tr> <tr> <td>13.</td> <td>slice()</td> <td>It returns the section fo an array in the new array.</td> </tr> <tr> <td>14.</td> <td>sort()</td> <td>It is used to sort the elements of an array.</td> </tr> <tr> <td>15.</td> <td>splice()</td> <td>It is used to add or remove the elements from an array.</td> </tr> <tr> <td>16.</td> <td>toString()</td> <td>It returns the string representation of an array.</td> </tr> <tr> <td>17.</td> <td>unshift()</td> <td>It is used to add one or more elements to the beginning of an array.</td> </tr> </table></arr_values.length;i++)></pre></arr.length;i++)>

Masyvo peržiūra naudojant for...in kilpą

Pavyzdys

 let i:any; let arr:string[] = [&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;]; for(i in arr) { console.log(arr[i]) } 

Išvestis:

 JavaTpoint 2300 Java Abhishek 

Masyvų perdavimas funkcijoms

Masyvus galime perduoti funkcijoms, nurodydami masyvo pavadinimą be indekso.

Pavyzdys

powershell didesnis nei arba lygus
 let arr:string[] = new Array(&apos;JavaTpoint&apos;, &apos;2300&apos;, &apos;Java&apos;, &apos;Abhishek&apos;); //Passing arrays in function function display(arr_values:string[]) { for(let i = 0;i <arr_values.length;i++) { console.log(arr[i]); } calling arrays in function display(arr); < pre> <p> <strong>Output:</strong> </p> <pre> JavaTpoint 2300 Java Abhishek </pre> <hr> <h2>TypeScript Spread operator</h2> <p>The spread operator is used to initialize arrays and objects from another array or object. We can also use it for object de-structuring. It is a part of the ES 6 version.</p> <p> <strong>Example</strong> </p> <pre> let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log(&apos;CopiedArray: &apos; +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log(&apos;NewArray: &apos; +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log(&apos;MergedArray: &apos; +mergedArray); </pre> <p> <strong>Output:</strong> </p> <pre> CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 </pre> <hr> <h2>Array Methods</h2> <p>The list of array methods with their description is given below.</p> <table class="table"> <tr> <th>SN</th> <th>Method</th> <th>Description</th> </tr> <tr> <td>1.</td> <td>concat()</td> <td>It is used to joins two arrays and returns the combined result.</td> </tr> <tr> <td>2.</td> <td>copyWithin()</td> <td>It copies a sequence of an element within the array.</td> </tr> <tr> <td>3.</td> <td>every()</td> <td>It returns true if every element in the array satisfies the provided testing function.</td> </tr> <tr> <td>4.</td> <td>fill()</td> <td>It fills an array with a static value from the specified start to end index.</td> </tr> <tr> <td>5.</td> <td>indexOf()</td> <td>It returns the index of the matching element in the array, otherwise -1.</td> </tr> <tr> <td>6.</td> <td>includes()</td> <td>It is used to check whether the array contains a certain element or not.</td> </tr> <tr> <td>7.</td> <td>Join()</td> <td>It is used to joins all elements of an array into a string.</td> </tr> <tr> <td>8.</td> <td>lastIndexOf()</td> <td>It returns the last index of an element in the array.</td> </tr> <tr> <td>9.</td> <td>Pop()</td> <td>It is used to removes the last elements of the array.</td> </tr> <tr> <td>10.</td> <td>Push()</td> <td>It is used to add new elements to the array.</td> </tr> <tr> <td>11.</td> <td>reverse()</td> <td>It is used to reverse the order of an element in the array.</td> </tr> <tr> <td>12.</td> <td>Shift()</td> <td>It is used to removes and returns the first element of an array.</td> </tr> <tr> <td>13.</td> <td>slice()</td> <td>It returns the section fo an array in the new array.</td> </tr> <tr> <td>14.</td> <td>sort()</td> <td>It is used to sort the elements of an array.</td> </tr> <tr> <td>15.</td> <td>splice()</td> <td>It is used to add or remove the elements from an array.</td> </tr> <tr> <td>16.</td> <td>toString()</td> <td>It returns the string representation of an array.</td> </tr> <tr> <td>17.</td> <td>unshift()</td> <td>It is used to add one or more elements to the beginning of an array.</td> </tr> </table></arr_values.length;i++)>

„TypeScript Spread“ operatorius

Sklaidos operatorius naudojamas masyvams ir objektams inicijuoti iš kito masyvo ar objekto. Taip pat galime naudoti objektų destruktūrizavimui. Tai yra ES 6 versijos dalis.

Pavyzdys

 let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log(&apos;CopiedArray: &apos; +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log(&apos;NewArray: &apos; +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log(&apos;MergedArray: &apos; +mergedArray); 

Išvestis:

 CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 

Masyvo metodai

Masyvo metodų sąrašas su jų aprašymu pateikiamas žemiau.

SN Metodas apibūdinimas
1. concat () Jis naudojamas sujungti du masyvus ir grąžina bendrą rezultatą.
2. copyWithin() Jis nukopijuoja elemento seką masyve.
3. kiekvienas () Jis grąžina teisingą, jei kiekvienas masyvo elementas atitinka pateiktą testavimo funkciją.
4. užpildyti () Jis užpildo masyvą statine verte nuo nurodyto pradžios iki pabaigos indekso.
5. indexOf() Jis grąžina atitinkančio elemento indeksą masyve, kitu atveju -1.
6. apima() Jis naudojamas patikrinti, ar masyve yra tam tikras elementas, ar ne.
7. Prisijunk () Jis naudojamas sujungti visus masyvo elementus į eilutę.
8. LastIndexOf() Jis grąžina paskutinį masyvo elemento indeksą.
9. Pop() Jis naudojamas pašalinti paskutinius masyvo elementus.
10. Push () Jis naudojamas naujų elementų įtraukimui į masyvą.
vienuolika. atvirkščiai () Jis naudojamas norint pakeisti elemento tvarką masyve.
12. Shift () Jis naudojamas pirmajam masyvo elementui pašalinti ir grąžinti.
13. gabalas () Jis grąžina masyvo skyrių naujame masyve.
14. Rūšiuoti () Jis naudojamas masyvo elementams rūšiuoti.
penkiolika. sandūra () Jis naudojamas elementams pridėti arba pašalinti iš masyvo.
16. toString() Tai grąžina masyvo eilutės atvaizdavimą.
17. unshift () Jis naudojamas vienam ar daugiau elementų pridėti prie masyvo pradžios.