Node.js lodash map() collect object attribute

Description

Node.js lodash map() collect object attribute


var _ = require('lodash');
var objects = [{one: 'hello', two: 'foo'}, {one: 'world', two: 'bar'}];

var resultOne = _.map(objects, function(elem) {
  return elem.one;
}); //from   w  w  w  . ja  v a  2  s .c o  m

var resultTwo = _.map(objects, function(elem) {
  return elem.two;
});

console.log(resultOne);
console.log(resultTwo);

More example


var lodash = require('lodash');
function timesThree(n) {
  return n * 3;/*from   w  w  w. j  av  a2s  .c om*/
}

var ary = lodash.map([1,2], timesThree);
console.log(ary);

var users = [
  { 'user': 'barney' },
  { 'user': 'fred' }
];

// using the `_.property` callback shorthand
var user = lodash.map(users, 'user');
console.log(user)
//  ['barney', 'fred']



PreviousNext

Related