Sequelize return array with Strings instead of Objects

Using Sequelize 3.13.0 it looks like it isn't possible to have find return a flat array of values rather than an array of objects.

One solution to your problem is to map the results using underscore or lodash:

AccountModel.findAll({
    where: {
        Age: {
            $gt : 18
        }
    },
    attributes: ['Name'],
    raw : true
})
.then(function(accounts) {
  return _.map(accounts, function(account) { return account.Name; })
})

I've uploaded a script that demonstrates this here.

As a quick note, setting raw: true makes the Sequelize find methods return plain old JavaScript objects (i.e. no Instance methods or metadata). This may be important for performance, but does not change the returned values after conversion to JSON. That is because Instance::toJSON always returns a plain JavaScript object (no Instance methods or metadata).


Here is a nice ES6 version of cfogelberg's answer using lambda expressions (Array.prototype.map() only works in IE9+ and lambda (arrow) functions have no IE support):

AccountModel.findAll({
    where: {
        Age: {
            $gt : 18
        }
    },
    attributes: ['Name'],
    raw : true
})
.then(accounts => accounts.map(account => account.Name));

Snippet (does not work in ie):

Here is a baby snippet I used for proof of concept. If it doesn't work, you are using one of the unsupported browsers mentioned above (and you shouldn't be making db calls directly from the browser anyway):

let objArray=[{key:1},{key:2},{key:3}];
console.log("Not IE friendly:");
console.log(objArray.map(obj => obj.key));
console.log("IE friendly (might even be ES3 if you change \"let\" to \"var\"):");
let names = [];
for(let i=0 ; i<objArray.length ; i++){
    names[i] = objArray[i].key
}
console.log(names)