假设你有CSV解析成的阵列的阵列,即, VAR数据= [[ “时间戳”,[ “temp1中”, “TEMP2”, “TEMP3”], [ “timestamp2”, [“temp4”,“temp5”,“temp6”], 等。]。
然后,我映射列行名称:
columns = ["timestamp", "temperature"];
,然后用它们来构建一个简单的字典:
var objectStore = db.transaction("temperatures", "readwrite").objectStore("temperatures");
for (var i = 0; i < data.length; i++) {
var data = {};
var row = data[i];
for (var j = 0; j < row.length; j++) {
data[columns[j]] = row[j];
}
objectStore.put(data, i);
}
这样你就可以用objectStore.get(rownumber)
访问行
但是为了让它更具有NoSQLish特性,我将其中一列(比如“timestamp”)作为对象的关键路径,并使用multiEntry来索引温度的所有子值数组。创建这样说:
db.createObjectStore("temperatures", {keyPath: "timestamp"});
db.createIndex("temp", "temperature", {"multiEntry": true});
然后,当你说,使用该密钥不打扰:
objectStore.put(data);
,那么你可以基于检索的东西:
objectStore.get("2012-04-05");
但是,什么是真正好的是,现在你的温度反转指数可以回溯到时间,让你可以说“给我所有温度在20到30度之间的记录”:
objectStore.index("temp").openCursor(IDBKeyRange.bound(20, 30)).onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
cursor.continue();
console.log("Found record: ", cursor.value);
}
}
您只需要小心将对象存储区的关键路径指向唯一可识别的列。
有点过时了,但我给他发了拉请求 –