Skip to content
Advertisement

how to use underscore .reduce to return and object that counts properties

considering

   var desserts = [
  {
    name: 'Chocolate Cake',
    ingredients: ['cocoa', 'flour', 'sugar', 'eggs', 'milk', 'butter' ],
    type: 'cake'
  },
  {
    name: 'Snickerdoodles',
    ingredients: ['flour', 'milk', 'butter', 'eggs', 'sugar', 'cinnamon', 'cream of tartar'],
    type: 'cookie'
  },
  {
    name: 'Strawberry-Rhubarb Pie',
    ingredients: ['flour', 'water', 'eggs', 'sugar', 'strawberries', 'rhubarb'],
    type: 'pie'
  },
  {
    name: 'Lemonade',
    ingredients: ['water', 'sugar', 'lemons'],
    type: 'drink'
  },
  {
    name: 'Chocolate Chip Cookies',
    ingredients: ['flour', 'butter', 'sugar', 'eggs', 'chocolate chips'],
    type: 'cookie'
  },
  {
    name: 'Apple Pie',
    ingredients: ['flour', 'water', 'cinnamon', 'apples', 'sugar'],
    type: 'pie'
  },
  {
    name: 'Apple Pie',
    ingredients: ['flour', 'water', 'cinnamon', 'apples', 'sugar'],
    type: 'pie'
  },
  {
    name: 'Angel Food Cake',
    ingredients: ['flour', 'eggs', 'sugar', 'cream of tartar'],
    type: 'cake'
  }
];

I am trying to use underscore reduce to output and object that counts how many of each type there are

{'pie' : 3, 'cake' : 2}... 

Here is what I have so far

var dessertCategories = function (desserts) {
  return _.reduce(desserts, function(memo, dessert) {
    var type1 = dessert.type;
    if (memo[type1] === undefined) {
      memo[type1] = 1;
    } else {
      memo[type1]++;
    }
    return memo;
  }, {});
};

I believe I am using reduce incorrectly here or it may be my logic. I am trying to follow the template from underscore.

Advertisement

Answer

Your issue, I think, is that you are not calling the dessertCategories function you’ve made anywhere.

The below gets you there. If you’d like to have dessertCategories be the map of categories->counts then remove the outer function(){...} wrapper.

var desserts = [
  {
    name: 'Chocolate Cake',
    ingredients: ['cocoa', 'flour', 'sugar', 'eggs', 'milk', 'butter' ],
    type: 'cake'
  },
  {
    name: 'Snickerdoodles',
    ingredients: ['flour', 'milk', 'butter', 'eggs', 'sugar', 'cinnamon', 'cream of tartar'],
    type: 'cookie'
  },
  {
    name: 'Strawberry-Rhubarb Pie',
    ingredients: ['flour', 'water', 'eggs', 'sugar', 'strawberries', 'rhubarb'],
    type: 'pie'
  },
  {
    name: 'Lemonade',
    ingredients: ['water', 'sugar', 'lemons'],
    type: 'drink'
  },
  {
    name: 'Chocolate Chip Cookies',
    ingredients: ['flour', 'butter', 'sugar', 'eggs', 'chocolate chips'],
    type: 'cookie'
  },
  {
    name: 'Apple Pie',
    ingredients: ['flour', 'water', 'cinnamon', 'apples', 'sugar'],
    type: 'pie'
  },
  {
    name: 'Apple Pie',
    ingredients: ['flour', 'water', 'cinnamon', 'apples', 'sugar'],
    type: 'pie'
  },
  {
    name: 'Angel Food Cake',
    ingredients: ['flour', 'eggs', 'sugar', 'cream of tartar'],
    type: 'cake'
  }
];
// Mock underscore implementation, ignore it.
var _ = {reduce: (arr, fn, init) => Array.prototype.reduce.call(arr, fn, init)}

var dessertCategories = function (desserts) {
  return _.reduce(desserts, function(memo, dessert) {
    var type1 = dessert.type;
    if (memo[type1] === undefined) {
      memo[type1] = 1;
    } else {
      memo[type1]++;
    }
    return memo;
  }, {});
};
// Actually calling the function you've created.
console.info(dessertCategories(desserts));
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement