Array adalah suatu tipe data terstruktur yang dapat menyimpan banyak data dengan suatu nama yang sama dan menempati tempat di memori yang berurutan serta bertipe data sama pula.
Contoh Array
x[0]=0; x[1]=1; x[2]=2; x[3]=3; x[4]=4; x[5]=5; for(i=0; i<6; i++) { document.writeln(x[i] + ' '); } /* Keluaran sebagai berikut… 0 1 2 3 4 5 */Membuat Array
var myArray = new Array(10); // contoh 1 var myArray = [ ]; // contoh 2 var arr1= new Array(10); // Membuat 10 Elament Array bernilai Null 0-9 var arr2= [10]; // Membuat 1 Elament Array. dimana 10 adalah Element PertamaInitial Array dan Menampilkannya
Contoh 1:
var myArray = ['Apel', 'Mangga', 'Jeruk'];
document.writeln('0-'+myArray[0]+''); // 0-Apel
document.writeln('1-'+myArray[1]+''); // 1-Mangga
document.writeln('2-'+myArray[2]+''); // 2-Jeruk
Contoh 2:
var myArray = [];
myArray[0] = 'Apel';
myArray[1] = 'Mangga';
myArray[2] = 'Jeruk';
document.writeln('0-'+myArray[0]+''); // 0-Apel
document.writeln('1-'+myArray[1]+''); // 1-Mangga
document.writeln('2-'+myArray[2]+''); // 2-Jeruk
Contoh 3:
var myArray = [ 3, 'hello!', function() {return 5}, {'color':'red', 'budget':25}];
document.writeln('0-'+myArray[0]+''); // 0-3
document.writeln('1-'+myArray[1]+''); // 1-hello!
document.writeln('2-'+myArray[2]()+''); // 2-5
document.writeln('3-'+myArray[3].color+''); // 3-red
document.writeln('3-'+myArray[3].budget+''); // 3-25
Contoh 4:
var myArray = [];
myArray ["one"] = "First";
myArray ["two"] = "Second";
myArray ["three"] = "Third";
for (i in myArray ) {
document.writeln(i+':'+myArray [i]+', ');
// Keluaran one:First, two:Second, three:Third
};
Array Multi Dimensi
var x=[0,1,2,3,4,5];
var y=[x];
document.writeln(y[0]); // 0,1,2,3,4,5
document.writeln(y[0][3]); // 2
Arrays Javascript Secara Default Mengacu pada Reference
Contoh 1:
var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
document.writeln(myArray[1]); //one
function ChangeVal(refArray) {
refArray[1] = 'changed';
}
ChangeVal(myArray);
document.writeln(myArray[1]); // changed
Contoh 2:
var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
var newArray= myArray;
newArray[1] = 'changed';
document.writeln(myArray[1]); //changed
Array slice()
Gunakan slice() agar tidak mereference ke Array lain.
var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
var newArray= myArray.slice();
newArray[1] = 'changed';
document.writeln(myArray[1]); //one
function passedByReference(refArray) {
refArray[1] = 'changed';
}
passedByReference(myArray.slice());
document.writeln(myArray[1]); // one
Array length
"length" Property pada array menghasilkan jumlah element pada suatu array.
for (var i=0; i
Array concat
var myArray = [1,2,3];
var newArray= [4,5,6];
var notArray= 7;
var joinedArray=myArray.concat(newArray, notArray);
document.writeln(myArray); // 1,2,3
document.writeln(joinedArray); // 1,2,3,4,5,6,7
Array every(function)
var isNumeric = function(x) {
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
return String(x).match(RegExp);
}
var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
document.writeln(myArray.every(isNumeric)); // true
var myArray = [1,2,3,4,5,6,7,8,9,'ten',11,12,13,14,15];
document.writeln(myArray.every(isNumeric)); // false
// Pada contoh diatas setiap element di check sampe menemukan nilai false
Array filter(function)
var isNumeric = function(x) {
// returns true if x is numeric and false if it is not.
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
return String(x).match(RegExp);
}
var myArray = [1,'two',3,'four',5,'six',7,'eight',9,'ten'];
var oddArray=myArray.filter(isNumeric);
document.writeln(oddArray); // 1,3,5,7,9
Array forEach(funcion)
var printArray = function (x, idx) {
document.writeln('['+idx+'] = '+x);
}
var myArray = [1,'dua',3,'empat',5];
myArray.forEach(printArray); // [0] = 1 [1] = dua[2] = 3 [3] = empat[4] = 5
Array join(delimeter)
var myArray = [1,'dua',3,'empat',5,'enam',7,'delapan',9,'sepuluh'];
var test1 = myArray.join();
var test2 = myArray.join(', ');
var test3 = myArray.join('+');
document.writeln(test1); // 1,dua,3,empat,5,enam,7,delapan,9,sepuluh
document.writeln(test2); // 1, dua, 3, empat, 5, enam, 7, delapan, 9, sepuluh
document.writeln(test3); // 1+dua+3+empat+5+enam+7+delapan+9+sepuluh
Arrray indexOf( )
var myArray = [1,'dua',3,'empat',5,'enam',7,'delapan',9,'sepuluh'];
document.writeln(myArray.indexOf('enam')); // 5
document.writeln(myArray.indexOf('satu')); // -1
Array lastIndexOf( )
var myArray = [1,'dua',3,'empat',5,'enam',7,'delapan',9,5,'sepuluh'];
document.writeln(myArray.lastIndexOf(5)); // 9
document.writeln(myArray.lastIndexOf('not here')); // -1
Array map(function)
var isNumeric = function(x) {
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
return String(x).match(RegExp);
}
var testElement = function(x) {
if (isNumeric(x)) {
return x;
} else {
return 0;
}
}
var myArray = [1,'dua',3,'empat',5,'enam',7,'delapan',9,'sepuluh'];
var newArray= myArray.map(testElement);
document.writeln(newArray); // outputs: 1,0,3,0,5,0,7,0,9,0
Array pop( )
Menghapus element terakhir
var myArray = [1,2,3,4,5,6,7,8,9,10];
var popped = myArray.pop();
document.writeln(popped); // 10
document.writeln(myArray); // 1,2,3,4,5,6,7,8,9
Array push( )
Menambahkan element di mulai dari element trakhir
var myArray = [1,2,3,4,5];
myArray.push(7,8,9,10);
document.writeln(myArray); // 1,2,3,4,5,6,7,8,9,10
document.writeln(myArray.length); // 10
Array reverse( )
Membalik susunan array
var myArray = [1,2,3,4,5,6,7,8,9,10];
document.writeln(myArray'); // 1,2,3,4,5,6,7,8,9,10
myArray.reverse();
document.writeln(myArray); // 10,9,8,7,6,5,4,3,2,1
Array shift( )
Menghapus element pertama
var myArray = [1,2,3,4,5,6,7,8,9,10];
theItem = myArray.shift();
document.writeln(theItem); // 1
document.writeln(myArray); // 2,3,4,5,6,7,8,9,10
Array slice( )
Mengambil value dari element array
var myArray = [1,2,3,4,5,6,7,8,9,10];
document.writeln(myArray); // 1,2,3,4,5,6,7,8,9,10
var newArray = myArray.slice();
document.writeln(newArray); // 1,2,3,4,5,6,7,8,9,10
var newArray = myArray.slice(5);
document.writeln(newArray); // 6,7,8,9,10
var newArray = myArray.slice(5,7);
document.writeln(newArray); // 6,7
Array some( )
var isNumeric = function(x) {
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
return String(x).match(RegExp);
}
var myArray = ['satu','dua','tiga','empat'];
document.writeln(myArray.some(isNumeric)); // false
var myArray = ['satu','dua','tiga','empat',5,'enam',7,'delapan','sembilan','sepuluh'];
document.writeln(myArray.some(isNumeric)); // true
Array sort( )
var myArray=[8,10,50,5,7,83,24,19,168];
myArray.sort()
document.writeln(myArray); // 10,168,19,24,5,50,7,8,83 (sorted alphabetically)
myArray.sort( function (a,b) { return a-b }); // Sort Numerically
document.writeln(myArray); //5,7,8,10,19,24,50,83,168
Array splice( )
var myArray=[1,2,3,4,5,6,7,8,9,10];
var newArray=[];
newArray = myArray.splice(5,1); //hapus satu item dari index ke lima
document.writeln(myArray); // 1,2,3,4,5,7,8,9,10
document.writeln(newArray); // 6
Array unshift( )
Menambahkan array di mulai dari element pertama
var myArray = [1,2,3,4,5,6,7,8,9,10];
document.writeln(myArray); // 1,2,3,4,5,6,7,8,9,10
myArray.unshift('a','b','c');
document.writeln(myArray); // a,b,c,1,2,3,4,5,6,7,8,9,10
0 komentar:
Posting Komentar