Convert array of objects to array of specific property from each object

I am using felixge/node-mysql to run this query: "SELECT DISTINCT tablename.id FROM tablename"

The rows returned are (obviously) in this format: [{id: 123},{id: 234},{id: 345}]

I can't figure out how to get this: [123, 234, 345]

With ES5, such is rather easy using a map transformation:

The map() method creates a new array with the results of calling a provided function on every element in this array.

For example:

var arr = [{id: 123},{id: 234},{id: 345}];
var res = arr.map(function (o) {
   return o.id;
});

Doing this "manually" with a loop would look like so:

var arr = [{id: 123},{id: 234},{id: 345}];
var res = [];
// Loop over the array
for (var i = 0; i < arr.length; i++) {
  var item = arr[i];
  // And add new value we want to result array
  res.push(item.id);
}

(Note that the variables are function-scoped, but I prefer to use "close placement" of var.)

You could use lodash or underscore:

var _ = require('lodash'); // or 'underscore'

var a = [{id: 123},{id: 234},{id: 345}];

var b = _.pluck(a, 'id');

console.log(b); // [123, 234, 345]