JSFiddle - React, Tailwind, and code Playground

by jpark7ca

JavaScript

var participants = [ { id : 1, chart_seat: 'j1', race_code: 3},  { id : 2, chart_seat: 'j2', race_code: 3},  { id : 3, chart_seat: 'p1', race_code: 1}, { id : 4, chart_seat: 'j3', race_code: 3}, { id : 5, chart_seat: 'j4', race_code: 1}];


var chartData = { options: { "title" : "Jury Composition - Race", "width" : 600, "height" : 300 }, data: [] };
var race_codes = [{ code: 1, descript: 'Black/African American', abbre: 'ba' },
                                    { code: 3, descript: 'White', abbre: 'w' },
                                    { code: 4, descript: 'Native Hawaiian/Pacific Island', abbre: 'np' },
                                    { code: 5, descript: 'American Indian/Alaska Native', abbre: 'aa' },
                                    { code: 6, descript: 'Asian', abbre: 'a' },
                                    { code: 7, descript: 'Other', abbre: 'o' },
                                    { code: 8, descript: 'Multi-Race', abbre: 'mr' },
                                    { code: 9, descript: 'Unknown', abbre: 'u' },
                                    { code: 100, descript: 'Hispanic', abbre: 'h' }];


function getCountByRace(race_codes, participants)
{
	var race_counts = [];
  
  for(var i=0; i < participants.length; i++){
    //alert(participants[i]);
    for(var j=0; j < race_codes.length; j++) {
      if(participants[i].race_code == race_codes[j].code){
              race_counts[race_codes[j].descript] = isNaN(race_counts[race_codes[j].descript]) ? 1 : race_counts[race_codes[j].descript] + 1;
      }
  	}
	}
  
  // 
  var race_counts_array = [];
  for (var property in race_counts) {
    if (race_counts.hasOwnProperty(property)) {
        //var item = {};
        //item[property] = race_counts[property];
        //race_counts_array.push(item);
        race_counts_array.push({ 0: property, 1: race_counts[property] });
    }
	}
  
  return race_counts_array;
}

chartData.data = getCountByRace(race_codes,...