Tag Archives: 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); […]

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) […]

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’ […]

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]; […]

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 […]

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 […]

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 […]

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, […]

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 […]

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 […]