Massivlar va satrlar (dasturlash)
Download 37.07 Kb.
|
20 MASSIVLAR VA SATRLAR
var fruits = ["Banana", "Orange", "Apple", "Mango"];
alert(fruits.length); // 4 alert(fruits.sort()); // ["Apple", "Banana", "Mango", "Orange"] var arr = [ 1, 2, 15 ]; alert( arr.sort() ) // 1, 15, 2 var index, text = ''; for(index = 0; index < fruits.length; index++) { text += fruits[index]; } var fruits = [] // bo'sh massiv fruits[1] = 'Peach' fruits[99] = 'Apple' alert(fruits.length) // 100 (lekin massivning 2 ta elementi aniqlangan holos) var emptyArray = []; var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] var emptyArray = []; var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] for(index = 0; index < 10; index++){ emptyArray[emptyArray.length] = Math.floor(Math.random() * 6) + 1 } for(index = 0; index < emptyArray.length; index++){ console.log(days[emptyArray[index]]); } length — bu yuqorida ko'rganingizdek, massiz uzunligi, elementlar sonini bildiradi. sort() — massiv elemntlarini alfabit bo'yich o'sish tartibida saralaydi. Etibor bergan bo'lsangiz yuqoridagi misollarda '15' '2' dan oldin turib qolgan, chunki saralsh paytida Javascript ularni oddiy char ( char bu bir simvol ) deb hisonlaydi va '15' dagi 1-sinvol '1' '2' bilan solishtiradi. Agarda siz qiymat bo'yicha saralash qilishni hohlasangin unda quyidagi namudandan foydalanishingiz mumkin: var fruits = ["Banana", "Orange", "Apple", "Mango"]; alert(fruits.length); // 4 alert(fruits.sort()); // ["Apple", "Banana", "Mango", "Orange"] var arr = [ 1, 2, 15 ]; alert( arr.sort() ) // 1, 15, 2 var index, text = ''; for(index = 0; index < fruits.length; index++) { text += fruits[index]; } var fruits = [] // bo'sh massiv fruits[1] = 'Peach' fruits[99] = 'Apple' alert(fruits.length) // 100 (lekin massivning 2 ta elementi aniqlangan holos) Download 37.07 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling