简体   繁体   中英

How do I access this javascript object and log it

I have been developing an app with ionic. And I have used SQLite. I have executed a query but I can't console log the value. Here's the code snippet:

var query = "SELECT SUM(total) FROM items";
            console.log(query);
            $cordovaSQLite.execute(db, query, []).then(function (res) {

                console.log(res.rows[0]);
                $scope.grand = {};
                $scope.grand = res.rows[0];

                console.log($scope.grand.SUM);

            }, function (err) {
                console.error("error=>" + err);
            }); 

I want to directly console log the value of SUM(total). But, the log shows like below:

 Object {SUM(total): 400}
    SUM(total):400

How do I directly console log 400?

I would guess it should work like this:

console.log(res.rows[0]['SUM(total)']);

Because I always feel uncomfortable using characters like parentheses in a key name I would maybe prefer:

var query = "SELECT SUM(total) as mytotal FROM items";

and then:

console.log(res.rows[0]['mytotal']);

or

console.log(res.rows[0].mytotal);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM