DEV Community

loading...

Discussion on: CRUD in JSON

Collapse
taufik_nurrohman profile image
Taufik Nurrohman
let data = {};
let id = Math.random();

// Create if not exists (primary key as object key)
!data[id] && (data[id] = {
    "nama": "Taufik",
    "asal": "?"
});

// Read
console.log(data[id] || {
    "nama": null,
    "asal": null
});

// Update
data[id] && Object.assign(data[id], {
    "nama": "Nur"
});

// Delete
delete data[id];
Enter fullscreen mode Exit fullscreen mode