Program JavaScript untuk Menambahkan Pasangan Kunci/Nilai ke Objek

0
(0)

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, kami menambahkan properti baru height ke person objek menggunakan notasi titik . yaitu person.height = 5.4;.


Contoh 2: Tambahkan Pasangan Kunci/Nilai ke Objek Menggunakan Notasi Kurung Kotak

// 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, kami menambahkan properti baru height ke person objek menggunakan notasi kurung siku [] yaitu person['height'] = 5.4;.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.


Notice: ob_end_flush(): failed to send buffer of zlib output compression (0) in /home/breatgem/widiyanata.com/wp-includes/functions.php on line 4755