Eduardo Farrera
Eduardo Farrera

Reputation: 216

Return array of values when querying single column instead of array of objects

When using the query builder in typeorm I do a select of a single column and I would like to get an array of values (In my particular case it would be an array of strings) but instead I get an array of objects where the key is the column name and the value is the row value.

For example the code:

this.createQueryBuilder('subscriptions')
  .select('id')
  .getRawMany()

Would return something like:

[{id:1},{id:2},{id:3}]

Instead of a simple array like [1,2,3]

Is there any way to obtain this array from the query? or the only way is to map the result of the query to extract the value?

Upvotes: 16

Views: 5326

Answers (1)

Muhammad Hassan Javed
Muhammad Hassan Javed

Reputation: 390

Only option you have is map

const arrayResults = queryResults.map(r => r.id);

Upvotes: 1

Related Questions