javascript json remove key

Posted in :

var json = { ... };
var key = "foo";
delete json[key]; // Removes json.foo from the dictionary.

呵呵, 真是太簡單了。

要增加也很簡單:
https://stackoverflow.max-everyday.com/2023/02/how-to-add-a-new-key-value-pair-in-existing-json-object-using-javascript/

類似從 array 插入(或取代)項目的是 array.splice
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// Inserts at index 1
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// Replaces 1 element at index 4
console.log(months);
// Expected output: Array ["Jan", "Feb", "March", "April", "May"]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *