键
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.)
但是在下面的示例中,两者的使用完全相同,我得到的值也相同:
- https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/primaryKey
- https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/key
那么实际区别是什么?
最佳答案
如果您遍历对象存储,它们是相同的。
如果您正在遍历索引,键
是索引键 而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/