gpt4 book ai didi

javascript - 如何使用 Dexie.js 删除值字段内的键/值?

转载 作者:行者123 更新时间:2023-12-02 22:57:47 34 4
gpt4 key购买 nike

我有一行的值如下:

{
"id": 1,
"token": "abcd"
}

如何删除并保存没有“ token ”的值,使其变成这样?

{
"id": 1
}

我需要先获取对象,修改它然后保存回来吗?

最佳答案

也许这会对您有所帮助:

function patch(db, id, delta) {
return new Promise((resolve, reject) => {
const tx = db.transaction('mystore', 'readwrite');
tx.onerror = (event) => reject(event.target.error);
tx.oncomplete = () => resolve();
const store = tx.objectStore('mystore');
const request = store.get(id);
request.onsuccess = (event) => {
const object = event.target.result;

if (!object) {
reject(new Error(`No matching object for ${id}`));
return;
}

for (const prop in delta) {
if (typeof delta[prop] === 'undefined') {
delete object[prop];
} else {
object[prop] = delta[prop];
}
}

store.put(object);
};
});
}

async function dostuff() {
let db;
const id = 1;
const delta = {
token: undefined
};

try {
db = await connect();
await patch(db, id, delta);
} finally {
if (db) {
db.close();
}
}
}

关于javascript - 如何使用 Dexie.js 删除值字段内的键/值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57907795/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com