Deary Cah Ndeso

Blog ini khusus untuk tutorial-tutorial , tips dan trik , wallpaper dll

JavaScript Metode Array

JavaScript Metode Array
Kekuatan JavaScript array terletak pada metode array.

Konversi Array ke String
Dalam JavaScript, semua benda memiliki valueOf () dan toString () metode.
The valueOf () metode adalah perilaku default untuk array. Ia mengembalikan sebuah array sebagai string:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.valueOf();


<!DOCTYPE html>
<html>
<body>

<p>The valueOf() method returns an array as a comma separated string.</p>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.valueOf();
</script>

</body>
</html>
Untuk JavaScript array, valueOf () dan toString () adalah sama.
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();


<!DOCTYPE html>
<html>
<body>

<p>The toString() method returns an array as a comma separated string.</p>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
</script>

</body>
</html>
The bergabung () metode juga bergabung semua elemen array menjadi string.
Ini berperilaku seperti toString (), tetapi Anda dapat menentukan pemisah:
Contoh
<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange","Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
</script>

<!DOCTYPE html>
<html>
<body>

<p>the join() method joins array elements into a string.</p>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
</script>

</body>
</html>


Popping dan Mendorong
Ketika Anda bekerja dengan array, mudah untuk menghapus elemen dan menambahkan elemen baru.
Ini adalah apa yang muncul dan mendorong adalah: Popping item dari array, atau mendorong barang ke dalam array.
The pop () metode menghapus elemen terakhir dari array:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();              // Removes the last element ("Mango") from fruits


<!DOCTYPE html>
<html>
<body>

<p>The pop method removes the last element from an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.pop()
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>
The dorongan () metode menambahkan elemen baru ke array (di akhir):
Description: Catatan
Ingat: [0] adalah elemen pertama dalam array. [1] adalah yang kedua. Array indeks mulai dengan 0.
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");       //  Adds a new element ("Kiwi") to fruits

<!DOCTYPE html>
<html>
<body>

<p>The push method appends a new element to an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.push("Kiwi")
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>
Pop () method mengembalikan string yang "muncul keluar".
Push () metode mengembalikan panjang array baru.

Pergeseran Elemen
Shifting setara dengan bermunculan, bekerja pada elemen pertama bukan yang terakhir.
The shift () metode menghapus elemen pertama dari array, dan "shift" semua elemen lain yang menempatkan ke bawah.
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();            // Removes the first element "Banana" from fruits

<!DOCTYPE html>
<html>
<body>

<p>The shift method removes (shifts) the first element of an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.shift();
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>
The unshift () metode menambahkan elemen baru ke array (di awal), dan "unshifts" unsur-unsur yang lebih tua:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon");    // Adds a new element "Lemon" to fruits

<!DOCTYPE html>
<html>
<body>

<p>The unshift method adds new elements to the beginning of an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.unshift("Lemon");
    document.getElementById("demo").innerHTML = fruits;
}
</script>

<p><b>Note:</b> The unshift() method does not work properly in Internet Explorer 8 and earlier, the values will be inserted, but the return value will be <em>undefined</em>.</p>

</body>
</html>
Pergeseran () method mengembalikan string yang "bergeser keluar".
The unshift () metode mengembalikan panjang array baru.

Mengubah Elemen
Elemen array diakses menggunakan mereka nomor indeks :
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[0] = "Kiwi";        // Changes the first element of fruits to "Kiwi"

<!DOCTYPE html>
<html>
<body>

<p>Array elements are accessed using their index number.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits[0] = "Kiwi";
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>
Properti Panjang menyediakan cara mudah untuk menambahkan elemen baru ke array:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Kiwi";          // Appends "Kiwi" to fruit

<!DOCTYPE html>
<html>
<body>

<p>The length property provides an easy way to append new elements to an array without using the push() method.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits[fruits.length] = "Kiwi";
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>


Menghapus Elemen
Karena JavaScript array obyek, elemen dapat dihapus dengan menggunakan operator JavaScript menghapus :
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0];           // Changes the first element in fruits to undefined

<!DOCTYPE html>
<html>
<body>

<p>Deleting an element leaves whole in an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    delete fruits[0];
    document.getElementById("demo").innerHTML = fruits[0];
}
</script>

</body>
</html>
Description: Catatan
Menggunakan hapus pada elemen array daun lubang terdefinisi dalam array. Gunakan pop () atau sambatan () sebagai gantinya.

Splicing Array
The sambatan () metode dapat digunakan untuk menambahkan item baru ke array:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");

<!DOCTYPE html>
<html>
<body>

<p>The splice() method adds new elements to an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
    fruits.splice(2, 0, "Lemon", "Kiwi");
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>

Parameter pertama (2) mendefinisikan posisi di mana unsur-unsur baru harus ditambahkan (disambung di).
Parameter kedua (0) mendefinisikan berapa banyak elemen harus dihapus .
Sisa parameter ("Lemon", "Kiwi") mendefinisikan unsur-unsur baru yang akan ditambahkan .

Menggunakan sambatan () Hapus Elemen
Dengan pengaturan parameter pintar, Anda dapat menggunakan sambatan () untuk menghapus elemen tanpa meninggalkan "lubang" dalam array:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(0,1);        // Removes the first element of fruits

<!DOCTYPE html>
<html>
<body>

<p>The splice() methods can be used to remove array elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
    fruits.splice(0, 1);
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>
Parameter pertama (0) mendefinisikan posisi di mana unsur-unsur baru harus ditambahkan (disambung di).
Parameter kedua (1) mendefinisikan berapa banyak elemen harus dihapus .
Sisa parameter dihilangkan. Tidak ada elemen baru akan ditambahkan.

Sorting Array
The sort () metode macam array abjad:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();            // Sorts the elements of fruits

<!DOCTYPE html>
<html>
<body>

<p>The sort() method sorts an array alphabetically.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.sort();
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>
Semacam () metode mengambil fungsi sebagai parameter. Fungsi ini dapat digunakan untuk menentukan metode semacam.

Membalikkan Array
The terbalik () metode membalikkan elemen dalam array.
Anda dapat menggunakannya untuk mengurutkan array dalam rangka menurun:
Contoh
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();            // Sorts the elements of fruits
fruits.reverse();         // Reverses the order of the elements

<!DOCTYPE html>
<html>
<body>

<p>The sort() method sorts an array alphabetically.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.sort();
    fruits.reverse();
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>


Numeric Urut
The sort () metode tidak dapat digunakan pada sejumlah array, karena macam abjad (25 lebih besar dari 100).
Anda dapat memperbaiki hal ini dengan menyediakan fungsi yang mengembalikan -1, 0, atau 1:
Contoh
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});

<!DOCTYPE html>
<html>
<body>

<p>Click the button to sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;   

function myFunction() {
    points.sort(function(a, b){return a-b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>
Gunakan trik yang sama untuk mengurutkan menurun array:
Contoh
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});

<!DOCTYPE html>
<html>
<body>

<p>Click the button to sort the array in descending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;

function myFunction() {
    points.sort(function(a, b){return b-a});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>
Fungsi Memanggil (a, b) mengembalikan -1, 0, atau 1, tergantung pada nilai-nilai a dan b.
Argumen disediakan oleh metode semacam () ketika membandingkan dua nilai.
Contoh: Ketika membandingkan 40 dan 100, semacam itu () metode panggilan function (40,100).

Temukan tertinggi (atau terendah) Nilai
Bagaimana menemukan nilai tertinggi dalam array?
Contoh
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});
// now points[0] contains the highest value

<!DOCTYPE html>
<html>
<body>

<p>The highest number is <span id="demo"></span></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});
document.getElementById("demo").innerHTML = points[0];
</script>

</body>
</html>
Dan terendah:
Contoh
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
// now points[0] contains the lowest value

<!DOCTYPE html>
<html>
<body>

<p>The lowest number is <span id="demo"></span></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
document.getElementById("demo").innerHTML = points[0];
</script>

</body>
</html>


Bergabung Array
The concat () metode menciptakan array baru dengan menggabungkan dua array:
Contoh
var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias","Linus"];
var myChildren = myGirls.concat(myBoys);     // Concatenates (joins) myGirls and myBoys

<!DOCTYPE html>
<html>
<body>

<p>Click "Try it" to display all my children:</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var myGirls = ["Cecilie", "Lone"];
    var myBoys = ["Emil", "Tobias", "Linus"];
    var myChildren = myGirls.concat(myBoys);
    document.getElementById("demo").innerHTML = myChildren;
}
</script>

</body>
</html>
The concat () metode dapat mengambil sejumlah argumen array:
Contoh
var arr1 = ["Cecilie", "Lone"];
var arr2 = ["Emil", "Tobias","Linus"];
var arr3 = ["Robin", "Morgan"];
var myChildren = arr1.concat(arr2, arr3);     // Concatenates arr1 with arr2 and arr3

<!DOCTYPE html>
<html>
<body>

<p>Click the button to join three arrays.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var arr1 = ["Cecilie", "Lone"];
    var arr2 = ["Emil", "Tobias", "Linus"];
    var arr3 = ["Robin", "Morgan"];
    document.getElementById("demo").innerHTML =
    arr1.concat(arr2, arr3);
}
</script>

</body>
</html>


Mengiris Array
The slice () metode irisan sepotong array:
Contoh
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);

<!DOCTYPE html>
<html>
<body>

<p>The slice() method slices elements from an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
    var citrus = fruits.slice(1,3);
    document.getElementById("demo").innerHTML = citrus;
}
</script>

</body>

</html>
Bagikan :
+
Previous
Next Post »
0 Komentar untuk "JavaScript Metode Array"

Powered by Blogger.
 
Template By Kunci Dunia
Back To Top