Count where for an indexedDB database
I am storing dates in an indexedDB database and I want to count the number of records that happened in 2018, 2017 etc and print them out in a table. The problem I'm having is I'm not sure how to check the dates against the previous records to count them cleanly. Its sort of the equivalent to a count where function is SQL.
At first, I had something like this but I'm not sure it's going to work. The dates are being stored like so 'Wed Jan 05 2018' hence the use of the Slice() method.
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()
}
If somebody could show me a simpler way to do this, I would appreciate it.
This is for Ryan:
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
{
}
}
javascript local-storage indexeddb
add a comment |
I am storing dates in an indexedDB database and I want to count the number of records that happened in 2018, 2017 etc and print them out in a table. The problem I'm having is I'm not sure how to check the dates against the previous records to count them cleanly. Its sort of the equivalent to a count where function is SQL.
At first, I had something like this but I'm not sure it's going to work. The dates are being stored like so 'Wed Jan 05 2018' hence the use of the Slice() method.
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()
}
If somebody could show me a simpler way to do this, I would appreciate it.
This is for Ryan:
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
{
}
}
javascript local-storage indexeddb
add a comment |
I am storing dates in an indexedDB database and I want to count the number of records that happened in 2018, 2017 etc and print them out in a table. The problem I'm having is I'm not sure how to check the dates against the previous records to count them cleanly. Its sort of the equivalent to a count where function is SQL.
At first, I had something like this but I'm not sure it's going to work. The dates are being stored like so 'Wed Jan 05 2018' hence the use of the Slice() method.
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()
}
If somebody could show me a simpler way to do this, I would appreciate it.
This is for Ryan:
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
{
}
}
javascript local-storage indexeddb
I am storing dates in an indexedDB database and I want to count the number of records that happened in 2018, 2017 etc and print them out in a table. The problem I'm having is I'm not sure how to check the dates against the previous records to count them cleanly. Its sort of the equivalent to a count where function is SQL.
At first, I had something like this but I'm not sure it's going to work. The dates are being stored like so 'Wed Jan 05 2018' hence the use of the Slice() method.
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()
}
If somebody could show me a simpler way to do this, I would appreciate it.
This is for Ryan:
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
{
}
}
javascript local-storage indexeddb
javascript local-storage indexeddb
edited Jan 3 at 21:55
Adam
asked Jan 2 at 22:58
AdamAdam
265
265
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
So I don't entirely know what the object store that holds this data looks like, so I might be making some assumptions regarding it and what you are doing.
First off, there is one better way to do what you're doing than opening up a cursor and iterating over the whole store. If you could restructure your data at all, you could create a field that stores the object's year, create an index on that field, and essentially do <ObjectStoreReference>.index('year').count()
to get all the documents very quickly.
But if you can't, then you'll need to do it this way. However, the way you are tracking the information right now using the cursor approach won't let you print out everything in a table, because each time the year you generate from cursor.value.date.slice(-4)
changes from your firstPostYear
, you clear your count.
You could try doing something like
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
And then you can loop over the entries in the object and print out each year and its count however you want.
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializingcounts
inside theonsuccess
handler of opening your cursor, in which casecursor.continue()
is re-initializing it on each successive iteration. You probably need to createcounts = {}
one level higher than the success handler.
– Ryan Dabler
Jan 3 at 20:20
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
Yeah, I think the problem is with this line:(counts[year]) + 1;
You're simply adding the number contained incounts[year]
to 1, but not assigning the result to anywhere. What happens if you change it tocounts[year] += 1
?
– Ryan Dabler
Jan 3 at 22:22
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014274%2fcount-where-for-an-indexeddb-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So I don't entirely know what the object store that holds this data looks like, so I might be making some assumptions regarding it and what you are doing.
First off, there is one better way to do what you're doing than opening up a cursor and iterating over the whole store. If you could restructure your data at all, you could create a field that stores the object's year, create an index on that field, and essentially do <ObjectStoreReference>.index('year').count()
to get all the documents very quickly.
But if you can't, then you'll need to do it this way. However, the way you are tracking the information right now using the cursor approach won't let you print out everything in a table, because each time the year you generate from cursor.value.date.slice(-4)
changes from your firstPostYear
, you clear your count.
You could try doing something like
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
And then you can loop over the entries in the object and print out each year and its count however you want.
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializingcounts
inside theonsuccess
handler of opening your cursor, in which casecursor.continue()
is re-initializing it on each successive iteration. You probably need to createcounts = {}
one level higher than the success handler.
– Ryan Dabler
Jan 3 at 20:20
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
Yeah, I think the problem is with this line:(counts[year]) + 1;
You're simply adding the number contained incounts[year]
to 1, but not assigning the result to anywhere. What happens if you change it tocounts[year] += 1
?
– Ryan Dabler
Jan 3 at 22:22
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
|
show 2 more comments
So I don't entirely know what the object store that holds this data looks like, so I might be making some assumptions regarding it and what you are doing.
First off, there is one better way to do what you're doing than opening up a cursor and iterating over the whole store. If you could restructure your data at all, you could create a field that stores the object's year, create an index on that field, and essentially do <ObjectStoreReference>.index('year').count()
to get all the documents very quickly.
But if you can't, then you'll need to do it this way. However, the way you are tracking the information right now using the cursor approach won't let you print out everything in a table, because each time the year you generate from cursor.value.date.slice(-4)
changes from your firstPostYear
, you clear your count.
You could try doing something like
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
And then you can loop over the entries in the object and print out each year and its count however you want.
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializingcounts
inside theonsuccess
handler of opening your cursor, in which casecursor.continue()
is re-initializing it on each successive iteration. You probably need to createcounts = {}
one level higher than the success handler.
– Ryan Dabler
Jan 3 at 20:20
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
Yeah, I think the problem is with this line:(counts[year]) + 1;
You're simply adding the number contained incounts[year]
to 1, but not assigning the result to anywhere. What happens if you change it tocounts[year] += 1
?
– Ryan Dabler
Jan 3 at 22:22
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
|
show 2 more comments
So I don't entirely know what the object store that holds this data looks like, so I might be making some assumptions regarding it and what you are doing.
First off, there is one better way to do what you're doing than opening up a cursor and iterating over the whole store. If you could restructure your data at all, you could create a field that stores the object's year, create an index on that field, and essentially do <ObjectStoreReference>.index('year').count()
to get all the documents very quickly.
But if you can't, then you'll need to do it this way. However, the way you are tracking the information right now using the cursor approach won't let you print out everything in a table, because each time the year you generate from cursor.value.date.slice(-4)
changes from your firstPostYear
, you clear your count.
You could try doing something like
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
And then you can loop over the entries in the object and print out each year and its count however you want.
So I don't entirely know what the object store that holds this data looks like, so I might be making some assumptions regarding it and what you are doing.
First off, there is one better way to do what you're doing than opening up a cursor and iterating over the whole store. If you could restructure your data at all, you could create a field that stores the object's year, create an index on that field, and essentially do <ObjectStoreReference>.index('year').count()
to get all the documents very quickly.
But if you can't, then you'll need to do it this way. However, the way you are tracking the information right now using the cursor approach won't let you print out everything in a table, because each time the year you generate from cursor.value.date.slice(-4)
changes from your firstPostYear
, you clear your count.
You could try doing something like
const counts = {};
if (cursor) {
const year = cursor.value.date.slice(-4);
if ( !(year in counts) ) {
counts[year] = 0;
}
counts[year]++;
cursor.continue();
}
And then you can loop over the entries in the object and print out each year and its count however you want.
edited Jan 2 at 23:44
answered Jan 2 at 23:39


Ryan DablerRyan Dabler
17117
17117
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializingcounts
inside theonsuccess
handler of opening your cursor, in which casecursor.continue()
is re-initializing it on each successive iteration. You probably need to createcounts = {}
one level higher than the success handler.
– Ryan Dabler
Jan 3 at 20:20
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
Yeah, I think the problem is with this line:(counts[year]) + 1;
You're simply adding the number contained incounts[year]
to 1, but not assigning the result to anywhere. What happens if you change it tocounts[year] += 1
?
– Ryan Dabler
Jan 3 at 22:22
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
|
show 2 more comments
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializingcounts
inside theonsuccess
handler of opening your cursor, in which casecursor.continue()
is re-initializing it on each successive iteration. You probably need to createcounts = {}
one level higher than the success handler.
– Ryan Dabler
Jan 3 at 20:20
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
Yeah, I think the problem is with this line:(counts[year]) + 1;
You're simply adding the number contained incounts[year]
to 1, but not assigning the result to anywhere. What happens if you change it tocounts[year] += 1
?
– Ryan Dabler
Jan 3 at 22:22
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Thank you for your reply. I don't want to change the data structure. On your example when I alert count after incrementing I get a value of 0. No matter how many records have the same year
– Adam
Jan 3 at 15:49
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializing
counts
inside the onsuccess
handler of opening your cursor, in which case cursor.continue()
is re-initializing it on each successive iteration. You probably need to create counts = {}
one level higher than the success handler.– Ryan Dabler
Jan 3 at 20:20
Can you share your code (either inline or in a Gist/JSFiddle, etc.)? I image the problem might be that you are initializing
counts
inside the onsuccess
handler of opening your cursor, in which case cursor.continue()
is re-initializing it on each successive iteration. You probably need to create counts = {}
one level higher than the success handler.– Ryan Dabler
Jan 3 at 20:20
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
I have added it along the bottom of my question
– Adam
Jan 3 at 21:55
Yeah, I think the problem is with this line:
(counts[year]) + 1;
You're simply adding the number contained in counts[year]
to 1, but not assigning the result to anywhere. What happens if you change it to counts[year] += 1
?– Ryan Dabler
Jan 3 at 22:22
Yeah, I think the problem is with this line:
(counts[year]) + 1;
You're simply adding the number contained in counts[year]
to 1, but not assigning the result to anywhere. What happens if you change it to counts[year] += 1
?– Ryan Dabler
Jan 3 at 22:22
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
Yeah. I should have noticed that one. The current data is 2017, 2017, 2018, 2018. Meaning the alerts should be 1, 2, 1, 2 but I'm getting the result 1, 1, 1, 1. I'm guessing this means the if statement isn't working?
– Adam
Jan 3 at 22:33
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014274%2fcount-where-for-an-indexeddb-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown