Tag: Array
Program JavaScript untuk Membagi Array menjadi Potongan yang Lebih Kecil
Contoh 1: Split Array Menggunakan slice() // program to split array into smaller chunks function splitIntoChunk(arr, chunk) { for (i=0; i < arr.length; i += chunk) { let tempArray; tempArray = arr.slice(i, i + chunk); console.log(tempArray); } } const array = [1, 2, 3, 4, 5, 6, 7, 8]; const chunk = 2; splitIntoChunk(array, chunk); […]
Read more »Program JavaScript Untuk Melakukan Persimpangan Antara Dua Array
Contoh 1: Lakukan Persimpangan Menggunakan Set // program to perform intersection between two arrays using Set // intersection contains the elements of array1 that are also in array2 function performIntersection(arr1, arr2) { // converting into Set const setA = new Set(arr1); const setB = new Set(arr2); let intersectionResult = []; for (let i of setB) […]
Read more »Program JavaScript untuk Mendapatkan Item Acak Dari Array
Contoh: Dapatkan Item Acak Dari Array // program to get a random item from an array function getRandomItem(arr) { // get random index value const randomIndex = Math.floor(Math.random() * arr.length); // get random item const item = arr[randomIndex]; return item; } const array = [1, ‘hello’, 5, 8]; const result = getRandomItem(array); console.log(result); Keluaran ‘hello’ […]
Read more »Program JavaScript untuk Membandingkan Elemen Dua Array
Contoh 1: Bandingkan Array Menggunakan JSON.stringify() // program to compare two arrays function compareArrays(arr1, arr2) { // compare arrays const result = JSON.stringify(arr1) == JSON.stringify(arr2) // if result is true if(result) { console.log(‘The arrays have the same elements.’); } else { console.log(‘The arrays have different elements.’); } } const array1 = [1, 3, 5, 8]; […]
Read more »Program JavaScript untuk Mengekstrak Nilai Properti yang Diberikan dari Objek sebagai Array
Contoh 1: Ekstrak Nilai Menggunakan map() // program to extract value as an array from an array of objects function extractValue(arr, prop) { // extract value from property let extractedValue = arr.map(item => item[prop]); return extractedValue; } const objArray = [{a: 1, b: 2}, {a: 4, b: 5}, {a: 8, b: 9}]; // passing an […]
Read more »Program JavaScript untuk Membuat Array Dua Dimensi
Contoh: Array Dua Dimensi Menggunakan untuk Loop // program to create a two dimensional array function twoDimensionArray(a, b) { let arr = []; // creating two dimensional array for (let i = 0; i< a; i++) { for(let j = 0; j< b; j++) { arr[i] = []; } } // inserting elements to array […]
Read more »Program JavaScript untuk Mengurutkan Array Objek berdasarkan Nilai Properti
Contoh 1: Urutkan Array berdasarkan Nama Properti // program to sort array by property name function compareName(a, b) { // converting to uppercase to have case-insensitive comparison const name1 = a.name.toUpperCase(); const name2 = b.name.toUpperCase(); let comparison = 0; if (name1 > name2) { comparison = 1; } else if (name1 < name2) { comparison […]
Read more »Program JavaScript untuk Menggabungkan Dua Array dan Menghapus Item Duplikat
Contoh 1: Menggunakan concat() dan untuk Loop // program to merge and remove duplicate value from an array function getUniqueAfterMerge(arr1, arr2){ // merge two arrays let arr = arr1.concat(arr2); let uniqueArr = []; // loop through array for(let i of arr) { if(uniqueArr.indexOf(i) === -1) { uniqueArr.push(i); } } console.log(uniqueArr); } const array1 = [1, […]
Read more »Program JavaScript untuk Menghapus Duplikat Dari Array
Contoh 1: Menggunakan indexOf() dan Push() // program to remove duplicate value from an array function getUnique(arr){ let uniqueArr = []; // loop through array for(let i of arr) { if(uniqueArr.indexOf(i) === -1) { uniqueArr.push(i); } } console.log(uniqueArr); } const array = [1, 2, 3, 2, 3]; // calling the function // passing array argument […]
Read more »Program JavaScript untuk Menambahkan Elemen ke Awal Array
Contoh 1: Tambahkan Elemen ke Array Menggunakan unshift() // program to add element to an array function addElement(arr) { // adding new array element arr.unshift(4); console.log(arr); } const array = [1, 2, 3]; // calling the function // passing array argument addElement(array); Keluaran [4, 1, 2, 3] Dalam program di atas, elemen baru ditambahkan ke […]
Read more »Program JavaScript untuk Mengosongkan Array
Contoh 1: Array Kosong dengan Mengganti Array Baru // program to empty an array function emptyArray(arr) { // substituting new array arr = []; return arr; } const array = [1, 2 ,3]; console.log(array); // call the function const result = emptyArray(array); console.log(result); Keluaran [1, 2, 3] [] Pada program di atas, nilai Himpunan digantikan […]
Read more »Program JavaScript untuk Memeriksa apakah Suatu Objek adalah Array
Contoh: Periksa Array Menggunakan Array.isArray() // program to check if an object is an array function checkObject(arr) { // check if arr is array const result = Array.isArray(arr); if(result) { console.log(`[${arr}] is an array.`); } else { console.log(`${arr} is not an array.`); } } const array = [1, 2, 3]; // call the function checkObject(array); […]
Read more »Program JavaScript untuk Menambahkan Objek ke Array
Contoh 1: Tambahkan Objek ke Array Menggunakan push() // program to append an object to an array function insertObject(arr, obj) { // append object arr.push(obj); console.log(arr); } // original array let array = [1, 2, 3]; // object to add let object = {x: 12, y: 8}; // call the function insertObject(array, object); Keluaran [1, […]
Read more »Program JavaScript untuk Memasukkan Item ke dalam Array
Contoh 1: Tambahkan Item ke Array Menggunakan splice() // program to insert an item at a specific index into an array function insertElement() { let array = [1, 2, 3, 4, 5]; // index to add to let index = 3; // element that you want to add let element = 8; array.splice(index, 0, element); […]
Read more »Program JavaScript untuk Memeriksa apakah Array Berisi Nilai Tertentu
Contoh 1: Periksa Array Menggunakan include() // program to check if an array contains a specified value const array = [‘you’, ‘will’, ‘learn’, ‘javascript’]; const hasValue = array.includes(‘javascript’); // check the condition if(hasValue) { console.log(‘Array contains a value.’); } else { console.log(‘Array does not contain a value.’); } Keluaran Array contains a value. Pada program […]
Read more »Program JavaScript untuk Menghapus Item Tertentu Dari Array
Contoh 1: Menggunakan For Loop // program to remove item from an array function removeItemFromArray(array, n) { const newArray = []; for ( let i = 0; i < array.length; i++) { if(array[i] !== n) { newArray.push(array[i]); } } return newArray; } const result = removeItemFromArray([1, 2, 3 , 4 , 5], 2); console.log(result); Keluaran […]
Read more »Program C ++ untuk Mengakses Elemen dari Array Menggunakan Pointer
Contoh: Mengakses Elemen Array Menggunakan Pointer #include using namespace std; int main() { int data[5]; cout > data[i]; cout
Read more »Program C ++ untuk Menggandakan Dua Matriks Menggunakan Array Multi-dimensi
Untuk mengalikan dua matriks, jumlah kolom dari matriks pertama harus sama dengan jumlah baris ke matriks kedua. Program ini menampilkan kesalahan sampai jumlah kolom matriks pertama sama dengan jumlah baris matriks kedua. Contoh: Gandakan dua matriks tanpa menggunakan fungsi #include using namespace std; int main() { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, […]
Read more »Program C ++ untuk Menambahkan Dua Matriks Menggunakan Array Multi-dimensi
Dalam program ini, pengguna diminta memasukkan jumlah baris r dan kolom c. Nilai dari r dan c harus kurang dari 100 dalam program ini. Pengguna diminta untuk memasukkan elemen dua matriks (urutan r * c). Kemudian, program menambahkan dua matriks ini, menyimpannya dalam matriks lain (array dua dimensi) dan menampilkannya di layar. Contoh: Tambahkan Dua […]
Read more »Program C ++ untuk Menemukan Elemen Terbesar dari Array
Program ini berlangsung n jumlah elemen dari pengguna (di mana, n ditentukan oleh pengguna) dan menyimpan data dalam array. Kemudian, program ini menampilkan elemen terbesar dari array menggunakan loop. Contoh: Menampilkan Elemen Terbesar dari sebuah array #include using namespace std; int main() { int i, n; float arr[100]; cout > n; cout
Read more »Program C ++ untuk Menghitung Rata-Rata Angka Menggunakan Array
Contoh: Hitung Rata-Rata Angka Menggunakan Array #include using namespace std; int main() { int n, i; float num[100], sum=0.0, average; cout > n; while (n > 100 || n
Read more »