JSFiddle - React, Tailwind, and code Playground

by fiddlebjoern

CSS

pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }

JavaScript

// generates random array with i items consisting 0-2 = 3 different values
var arr = []
, i = 100

while(i--) {
    arr[i]=getRandomInt(0,2)
}

function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
    
document.write(arr.join())
    
// my part so far
var historyPartLength = arr.length
, indexedarr = {}
, i
    

// this produces some object, but i need an array ...    
for(i=0;i<historyPartLength;i++) {
    indexedarr[arr[i]] ? 
    indexedarr[arr[i]].push(arr[i]) :
    indexedarr[arr[i]] = []
    }


    output(JSON.stringify(indexedarr, undefined, 4));

    


function output(inp) {
    document.body.appendChild(document.createElement('pre')).innerHTML = inp;
}