I'm trying to use $.each to speed up iteration of a SqlResultsetRowList where I'm currently using a For loop. Here is the new code -
localDB.transaction(function(transaction){
transaction.executeSql(query, [val], function(transaction, results)
{
var rows = results.rows;
$.each(rows, function(i) {
var row = results.rows.item(i);
});
}
)
});
The problem is i
is being returned as not the index but the string "length" and it obviously breaks at that point.
I've done some further testing and this works as expected in Chrome. Chrome sees the SqlResultsetRowList as an array but Safari doesn't. Is it possible to maybe convert the result set to an array so Safari can iterate it using $.each?
I'm trying to use $.each to speed up iteration of a SqlResultsetRowList where I'm currently using a For loop. Here is the new code -
localDB.transaction(function(transaction){
transaction.executeSql(query, [val], function(transaction, results)
{
var rows = results.rows;
$.each(rows, function(i) {
var row = results.rows.item(i);
});
}
)
});
The problem is i
is being returned as not the index but the string "length" and it obviously breaks at that point.
I've done some further testing and this works as expected in Chrome. Chrome sees the SqlResultsetRowList as an array but Safari doesn't. Is it possible to maybe convert the result set to an array so Safari can iterate it using $.each?
Share Improve this question edited May 21, 2015 at 20:35 rayt asked May 21, 2015 at 19:49 raytrayt 1941 silver badge8 bronze badges 2- Hi. You should add a tag for SqlResultsetRowList to this question. Since not all the properties/methods aren't shared in base JavaScript, only someone who knows SqlResultsetRowList will be able to answer your question. – Kathy Commented May 21, 2015 at 20:05
- Also, why are you defining the variable "rows" as results.rows, then not using it when you define the variable "row"? You might also want to change the name of the "rows" variable to something that isn't the same as a method you're using. – Kathy Commented May 21, 2015 at 20:07
2 Answers
Reset to default 9So again this seems to only affect Safari and Mobile Safari and I have found the following to suffice for what I was looking to do. Adding for anyone possibly in the same situation.
Since Safari doesn't see a SqlResultsetRowList as an array you need to create one from it.
var contacts = [];
for (i = 0; i < results.rows.length; i++){
contacts.push(results.rows.item(i));
}
Now that you have the array created from the results you can iterate contacts
using $.each
.
$.each(contacts, function() {
var ID = this.Contact_ID;
//more logic here
});
Try this
$.each(rows, function(i, val) {
var row = results.rows.item[i];
console.log('index:' +i+ ' val:' +val);
});