Program JavaScript untuk Menghapus Properti dari Objek

0
(0)

Sebuah objek ditulis dalam a kunci / nilai pasangan. Itu kunci / nilai pasangan disebut properti. Sebagai contoh,

const student = {
    name: 'John',
    age: 22
}

Sini, name: 'John' dan age: 22 adalah dua sifat objek siswa.


Contoh: Menghapus Properti Dari Objek

// program to remove a property from an object

// creating an object
const student = { 
    name: 'John',
    age: 20,
    hobbies: ['reading', 'games', 'coding'],
    greet: function() {
        console.log('Hello everyone.');
    },
    score: {
        maths: 90,
        science: 80
    }
};

// deleting a property from an object
delete student.greet;
delete student['score'];

console.log(student);

Keluaran

{
  age: 20,
  hobbies: ["reading", "games", "coding"],
  name: "John"
}

Dalam program di atas, file delete operator digunakan untuk menghapus properti dari objek.

Anda bisa menggunakan delete operator dengan . atau [ ] untuk menghapus properti dari objek.

Catatan: Anda tidak boleh menggunakan operator delete pada properti objek JavaScript yang telah ditentukan sebelumnya.

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.