JSFiddle - React, Tailwind, and code Playground

by Matthew Day

HTML

<div ng-app="myApp">
  <div ng-controller="myCtrl as vm">
    <h3>An array of objects</h3>
    <pre>{{vm.people | json}}</pre>
    <hr />
    <h3>Final Object Showing Arrays as Values of Object Keys</h3>
    <pre>{{vm.obj | json}}</pre>
  </div>
</div>

CSS

* {
  font-family: 'Arial', sans-serif;
}

JavaScript

angular.module('myApp', []);

angular.module('myApp')
.controller('myCtrl', function() {
	var vm = this;
  
// define an array of objects to work with
  vm.people = [{
    	"name": "Ana",
      "belongsto": 0,
      "has": 1
    }, {
    	"name": "Billy",
      "belongsto": 1,
      "has": 34
    }, {
    	"name": "Carlos",
      "belongsto": 1,
      "has": 52
    }, {
      "name": "Channing",
      "belongsto": 1,
      "has": 37
    }, {
    	"name": "David",
      "belongsto": 34,
      "has": 100
    }, {
    	"name": "Eunice",
      "belongsto": 34,
      "has": 100
    }, {
    	"name": "Fatima",
      "belongsto": 52,
      "has": 100
    }, {
    	"name": "Grace",
      "belongsto": 52,
      "has": 100
     }, {
    	"name": "Harriet",
      "belongsto": 1,
      "has": 100
  }]

var numbersArray = [];  // to store numbers and help us find objects that have the same number
vm.obj = {};  // the final object that we want

function equals(number, name) {
	if(number == 0 || number == undefined || number == null) {
  	// do nothing if the number is something we don't care about
  } else if(numbersArray.length === 0) {
  	// since the numbers array is empty, push our first number to it
  	numbersArray.push(number);
    // next define the number as a key in our object and give it an array as its property
    vm.obj[number] = [];
    // then push the name associated with this number to the array we just created
    vm.obj[number].push(name);
  } else if(numbersArray.length > 0 && numbersArray.indexOf(number) != -1) {
    // this is the only 'if' block where we push the name to an array that has already been defined; this means that the code has determined the number we are dealing with is already found in the numbers array
    vm.obj[number].push(name);
  } else if(numbersArray.length > 0 && numbersArray.indexOf(number) == -1) {
  	// if a number is not found in the numbers array, add it to the numbers array
    numbersArray.push(number);
    // since this is...