JSFiddle - React, Tailwind, and code Playground

by josekerekes

HTML

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<button id="clicker1">
  push it
</button>
<button id="clicker2">
  push it
</button>
<div id="target">
</div>
</div>

JavaScript

var arr = [];
var tempArr = [];

$('#clicker1').click(function() {
	compileChart('categ1');
  console.log(arr);
});

$('#clicker2').click(function() {
	compileChart('categ2');
  console.log(arr)
});


function compileChart(categ) {
  var obj = function() {
    this.name = "Jozsica",
    this.age = 18,
    this.category = categ
	}

	var newobj = new obj();

  
	drawChart(categ, newobj) ;
}

function drawChart(categ, obj) {
	  if(arr.length) {
    	 console.log('d')
        for(i=0;i<arr.length;i++) {
          
          if(arr[i].category != categ) {
            tempArr.push(obj)
          }
        }
    }
    else {
    	tempArr.push(obj)
    }
    
    arr = [].concat(tempArr);
    tempArr.length = 0;
   
}