计算 indexedDB 数据库的位置
Count where for an indexedDB database
我将日期存储在 indexedDB 数据库中,我想计算 2018 年、2017 年等发生的记录数,并将它们打印在 table 中。我遇到的问题是我不确定如何根据以前的记录检查日期以清楚地计算它们。它相当于函数为 SQL.
的计数
起初,我有类似的东西,但我不确定它是否会起作用。日期的存储方式如此 'Wed Jan 05 2018' 因此使用了 Slice() 方法。
var cursor = event.target.result;
//2016
firstPostYear = cursor.value.date.slice(-4);
alert(firstPostYear);
var count = 0;
if (cursor)
{
if (cursor.value.date.slice(-4) === firstPostYear)
{
count += 1;
}
else
{
console.log(count);
count = 0;
firstPostYear = cursor.value.date.slice(-4)
}
cursor.continue()
}
如果有人能告诉我一个更简单的方法来做到这一点,我将不胜感激。
这是给瑞安的:
function filterSelection(filter)
{
var objectStore = db.transaction('posts').objectStore('posts');
objectStore.openCursor().onsuccess = function (event)
{
var cursor = event.target.result;
if (filter === "all")
{
//Get the number of all time posts
var transaction = db.transaction(['posts'], 'readonly');
var objectStore = transaction.objectStore('posts');
var countRequest = objectStore.count();
countRequest.onsuccess = function()
{
// Get table element
var table = document.getElementById("activityLog");
//Get table length
var rowCount = table.rows.length;
// Create a table row
var row = table.insertRow(rowCount);
// Insert new cells positions 1-5 of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Add some text to the new cells:
cell1.innerHTML = "All time";
cell2.innerHTML = countRequest.result;
}
}
else if (filter === "Years")
{
//Get the number of posts each year
var cursor = event.target.result;
var counts = {};
if (cursor)
{
const year = cursor.value.date.slice(-4);
if ( !(year in counts) )
{
counts[year] = 0;
}
(counts[year]) + 1;
alert(counts[year]);
cursor.continue();
}
else
{
}
}
所以我不完全知道保存这些数据的对象存储是什么样的,所以我可能会对此做出一些假设以及你在做什么。
首先,有一种比打开游标并遍历整个商店更好的方法来完成您正在做的事情。如果您完全可以重组数据,则可以创建一个字段来存储对象的年份,在该字段上创建一个索引,然后 <ObjectStoreReference>.index('year').count()
以非常快速地获取所有文档。
但如果你做不到,那么你就需要这样做。但是,您现在使用游标方法跟踪信息的方式不会让您打印出 table 中的所有内容,因为每次从 cursor.value.date.slice(-4)
生成的年份都会从 [=13] 更改=], 你清除你的计数。
你可以尝试做类似的事情
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
然后您可以遍历对象中的条目并根据需要打印出每年及其计数。
我将日期存储在 indexedDB 数据库中,我想计算 2018 年、2017 年等发生的记录数,并将它们打印在 table 中。我遇到的问题是我不确定如何根据以前的记录检查日期以清楚地计算它们。它相当于函数为 SQL.
的计数起初,我有类似的东西,但我不确定它是否会起作用。日期的存储方式如此 'Wed Jan 05 2018' 因此使用了 Slice() 方法。
var cursor = event.target.result;
//2016
firstPostYear = cursor.value.date.slice(-4);
alert(firstPostYear);
var count = 0;
if (cursor)
{
if (cursor.value.date.slice(-4) === firstPostYear)
{
count += 1;
}
else
{
console.log(count);
count = 0;
firstPostYear = cursor.value.date.slice(-4)
}
cursor.continue()
}
如果有人能告诉我一个更简单的方法来做到这一点,我将不胜感激。
这是给瑞安的:
function filterSelection(filter)
{
var objectStore = db.transaction('posts').objectStore('posts');
objectStore.openCursor().onsuccess = function (event)
{
var cursor = event.target.result;
if (filter === "all")
{
//Get the number of all time posts
var transaction = db.transaction(['posts'], 'readonly');
var objectStore = transaction.objectStore('posts');
var countRequest = objectStore.count();
countRequest.onsuccess = function()
{
// Get table element
var table = document.getElementById("activityLog");
//Get table length
var rowCount = table.rows.length;
// Create a table row
var row = table.insertRow(rowCount);
// Insert new cells positions 1-5 of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Add some text to the new cells:
cell1.innerHTML = "All time";
cell2.innerHTML = countRequest.result;
}
}
else if (filter === "Years")
{
//Get the number of posts each year
var cursor = event.target.result;
var counts = {};
if (cursor)
{
const year = cursor.value.date.slice(-4);
if ( !(year in counts) )
{
counts[year] = 0;
}
(counts[year]) + 1;
alert(counts[year]);
cursor.continue();
}
else
{
}
}
所以我不完全知道保存这些数据的对象存储是什么样的,所以我可能会对此做出一些假设以及你在做什么。
首先,有一种比打开游标并遍历整个商店更好的方法来完成您正在做的事情。如果您完全可以重组数据,则可以创建一个字段来存储对象的年份,在该字段上创建一个索引,然后 <ObjectStoreReference>.index('year').count()
以非常快速地获取所有文档。
但如果你做不到,那么你就需要这样做。但是,您现在使用游标方法跟踪信息的方式不会让您打印出 table 中的所有内容,因为每次从 cursor.value.date.slice(-4)
生成的年份都会从 [=13] 更改=], 你清除你的计数。
你可以尝试做类似的事情
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
然后您可以遍历对象中的条目并根据需要打印出每年及其计数。