gpt4 book ai didi

javascript - IndexedDB的IDBCursor中key和primaryKey有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:42:23 25 4
gpt4 key购买 nike

documentation here says that

Returns the cursor's current key. (Cursors also have a key and a value which represent the key and the value of the last iterated record.)

主键

Returns the cursor's current effective key. (If the source of a cursor is an object store, the effective object store of the cursor is that object store and the effective key of the cursor is the cursor's position.)

但是在下面的示例中,两者的使用完全相同,我得到的值也相同:

那么实际区别是什么?

最佳答案

如果您遍历对象存储,它们是相同的。

如果您正在遍历索引,索引键primaryKey对象中的键商店

例如:

 book_store = db.createObjectStore('books');
title_index = store.createIndex('by_title', 'title');

key = 123;
value = {title: 'IDB for Newbies', author: 'Alice'};
book_store.put(value, key);

book_store.openCursor().onsuccess = function(e) {
cursor = e.target.result;
console.log(cursor.key); // logs 123
console.log(cursor.primaryKey); // logs 123
};
title_index.openCursor().onsuccess = function(e) {
cursor = e.target.result;
console.log(cursor.key); // logs 'IDB for Newbies'
console.log(cursor.primaryKey); // logs 123
};

关于javascript - IndexedDB的IDBCursor中key和primaryKey有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37971117/

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