plastikaweb
1/3/2017 - 5:11 PM

JS Bin Reducing an Array into an Object // source https://jsbin.com/hesivok

JS Bin

Reducing an Array into an Object

// source https://jsbin.com/hesivok

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Reducing an Array into an Object">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

<script id="jsbin-javascript">
var votes = [
  'angular',
  'angular',
  'react',
  'react',
  'vanilla',
  'angular',
  'angular',
  'vanilla',
  'vanilla',
  'angular',
  'react',
  'react',
  'react',
  'backbone',
  'react',
  'ember',
];

var initialValue = {};

var reducer = function (tally, vote) {
  if(!tally[vote]) {
    tally[vote] = 1;
  } else {
    tally[vote]++;
  }
  return tally;
};


var total = votes.reduce(reducer, initialValue);

console.log(total);
</script>



<script id="jsbin-source-javascript" type="text/javascript">var votes = [
  'angular',
  'angular',
  'react',
  'react',
  'vanilla',
  'angular',
  'angular',
  'vanilla',
  'vanilla',
  'angular',
  'react',
  'react',
  'react',
  'backbone',
  'react',
  'ember',
];

var initialValue = {};

var reducer = function (tally, vote) {
  if(!tally[vote]) {
    tally[vote] = 1;
  } else {
    tally[vote]++;
  }
  return tally;
};


var total = votes.reduce(reducer, initialValue);

console.log(total);</script></body>
</html>
var votes = [
  'angular',
  'angular',
  'react',
  'react',
  'vanilla',
  'angular',
  'angular',
  'vanilla',
  'vanilla',
  'angular',
  'react',
  'react',
  'react',
  'backbone',
  'react',
  'ember',
];

var initialValue = {};

var reducer = function (tally, vote) {
  if(!tally[vote]) {
    tally[vote] = 1;
  } else {
    tally[vote]++;
  }
  return tally;
};


var total = votes.reduce(reducer, initialValue);

console.log(total);