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 […]
Tag Archives: Objek
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 […]
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); […]
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, […]
Contoh 1: Konversi Objek ke String Menggunakan JSON.stringify() // program to convert an object to a string const person = { name: ‘Jack’, age: 27 } const result = JSON.stringify(person); console.log(result); console.log(typeof result); Keluaran {“name”:”Jack”,”age”:27} string Dalam contoh di atas, JSON.stringify() metode yang digunakan untuk mengubah objek menjadi string. Itu typeof operator memberikan tipe data […]
Contoh 1: Tambahkan Pasangan Kunci/Nilai ke Objek Menggunakan Notasi Titik // program to add a key/value pair to an object const person = { name: ‘Monica’, age: 22, gender: ‘female’ } // add a key/value pair person.height = 5.4; console.log(person); Keluaran { name: “Monica”, age: 22, gender: “female”, height: 5.4 } Dalam contoh di atas, […]
Contoh 1: Hitung Jumlah Kunci dalam Objek Menggunakan for…in // program to count the number of keys/properties in an object const student = { name: ‘John’, age: 20, hobbies: [‘reading’, ‘games’, ‘coding’], }; let count = 0; // loop through each key/value for(let key in student) { // increase the count ++count; } console.log(count); Keluaran […]
Contoh 1: Gabungkan Properti Dua Objek Menggunakan Object.assign() // program to merge property of two objects // object 1 const person = { name: ‘Jack’, age:26 } // object 2 const student = { gender: ‘male’ } // merge two objects const newObj = Object.assign(person, student); console.log(newObj); Keluaran { name: “Jack”, age: 26, gender: “male” […]
Contoh 1: Perulangan Melalui Objek Menggunakan untuk … in // program to loop through an object using for…in loop const student = { name: ‘John’, age: 20, hobbies: [‘reading’, ‘games’, ‘coding’], }; // using for…in for (let key in student) { let value; // get the value value = student[key]; console.log(key + ” – ” […]
Objek JavaScript adalah tipe data kompleks yang dapat berisi berbagai tipe data. Sebagai contoh, const person = { name: ‘John’, age: 21, } Sini, person adalah sebuah objek. Sekarang, Anda tidak dapat mengkloning objek dengan melakukan sesuatu seperti ini. const copy = person; console.log(copy); // {name: “John”, age: 21} Pada program di atas, copy variabel […]
- 1
- 2