1

Here is an excerpt from a Node Webkit WebSQL wrapper I'm building and I'm running into a problem. See the <---- line below.

get_columns is a simple JavaScript array:

['id','group_name','description']

But when they come out of the database the object (a.k.a. results.rows.item(i)):

{'description','group_name','id'}

Is this because the browser or JavaScript wants to sort all objects in alphabetical order?

db.transaction(function(tx) {
    var sql = 'SELECT ' + get_columns + ' FROM ' + table;
    tx.executeSql(sql, [], function(tx, results) {
        if (results.rows.length) {
            for (var i = 0; i < results.rows.length; i++) {
                _data.push(results.rows.item(i)); // <---- columns from WebSQL are in alphabetical order, so not cool. 
            }
        }
        deferred.resolve(_data);
    });
});

My thoughts are to process the object and put the values of the object into my array as values for each key.

Qantas 94 Heavy
  • 14,790
  • 31
  • 61
  • 78
Robbie Smith
  • 178
  • 1
  • 6
  • 1
    related: http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – epascarello Feb 14 '14 at 21:04
  • So my hunch is correct then. I'll have to interpret the resulting object and do a 1:1 matching on expected columns and assign values. – Robbie Smith Feb 14 '14 at 21:59
  • What exactly is the problem? If it's an object anyway, you are going to access it by property names. Or do you want to push an array for each row to `_data`? – Bergi Jul 30 '14 at 18:39

1 Answers1

1

Use JSON.stringify with the results as the first parameter and the array as a second parameter to enforce the order:

function foo(get_columns)
  {
  var results = {"a":1,"b":2,"c":3};

  _data = JSON.stringify(results, get_columns);
  return _data;
  }

foo(['c','a','b'])

References

Paul Sweatte
  • 22,871
  • 7
  • 116
  • 244