JSFiddle - React, Tailwind, and code Playground

by jsumners

JavaScript

var myTmpObj = {},
    myArray = [],
    i = 0, j = 0;

while (i < 4) {
    j = Math.floor(Math.random()*33);
    
    // Notice that we can use the "in" operator with an object
    if (j > 0 && !(j in myTmpObj)) {
        myTmpObj[j] = j;
        i += 1;
    }
}

// We now have a sparse "array"
console.dir(myTmpObj);

// If we really want it as an array...
i = 0;
for (var prop in myTmpObj) {
    if (myTmpObj.hasOwnProperty(prop)) {
        myArray[i] = prop;
        i += 1;
    }
}

console.dir(myArray);