0

I want to store a random generated data that looks like 1-2-3-4-5-6 using a javascript array.

Each data may contain only the - character and 6 unique numbers from 1 to 49.

I declared the array as: var a = new Array();

Each time a new data is being generated, I store it like: a[data] = 1; where data might be 2-36-33-21-9-41 and 1 represents the ratio;

If the data generated already exists, I must increment the ratio, like: a[data]++;

Why is the length property not available?

I need to refresh the length value on the page each time a new unique data has been generated, maybe at each 1 millisecond.

I can't parse a 1 million array that fast ... each time ...

this is how i insert or update:

if (variante_unice[s]) {
                                variante_unice[s]++;
                            }
                            else
                            {
                                variante_unice[s] = 1;
                                variante_unice_total++;
                            }

the code:

    window.setInterval(
                            function() {
                                variante++;
                                $("#variante").text(variante);
                                for (j = 1; j <= 49; j++) {
                                    $("#v" + j).attr("class", "");
                                }
                                for (j = 1; j <= 49; j++) {
                                    extracted[j] = 0;
                                }
                                ind = 0;
                                s = '';
                                while (ind < 6) {
                                    r = Math.floor((Math.random() * 49) + 1);
                                    if (extracted[r] == 0) {
//this is where i generate the data
                                        if (s === '') {
                                            s = r;
                                        }
                                        else
                                        {
                                            s += '-' + r;
                                        }
                                        //console.log(r);
                                        extracted[r] = 1;
                                        frecvency[r]++;
                                        $("#n" + r).attr("class", "green");
                                        $("#v" + r).attr("class", "green");
                                        ind++;
                                        //console.log('i'+frecventa[r]);
                                        frecventa[r]++;
                                        //console.log('d'+frecventa[r]);
                                        $("#f" + r).text(frecventa[r]);
                                    }
                                }
//is the generated data new? if not, increment ratio
                                if (variante_unice[s]) {
                                    variante_unice[s]++;
                                }
                                else
                                {
                                    variante_unice[s] = 1;
                                    variante_unice_total++;
                                }
                                //console.log(variante_unice);
                                //console.log(variante_unice[s]);
                                //console.log(variante_unice.length);
                                //console.log(s);
                                verifica_varianta();
                                //console.log(extracted);
                                //console.log(frecvency);
                            }
                    , 1);
Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

1 Answers1

4

If you are adding elements to you array with a["1-2-3-4-5-6"] = 1;, you are not really appending it to the array, but creating a property to the object, that the array is. You can add any property to the array, but it will not be counted into the length property unless you use a numeric key (0,1,2,3...). So, you have bascially two options now:

1) leave your data structue as it is and compute the length by yourself:

If you don't need tu support IE8 you can use Object.keys(a).length, or you write a helperfunction like this:

function computeLength(obj) {
    var result = 0;
    for (var i in obj) {
        result++;
    }
    return result;
}

If you want to use one of those methods, I would also suggest you are declaring an object instead of an array: var a = {};

2) Use a proper array

In an array you don't have key-value pairs but index-value pairs. Since you want to store two different values, you could store those values in an object, and append this object to your array, which leads to an array of objects:

var a = [];
a.push({data: data, counter: 1});

Using this method, you will however no longer be able to access your data with the "1-2-3-4-5-6" string. If you want to access an item, you have to provide the index of the item in the array.

basilikum
  • 10,378
  • 5
  • 45
  • 58
  • I use this and it does not work; var count = Object.keys(variante_unice).length; $("#variante_unice").text(count); – Ionut Flavius Pogacian Jul 28 '13 at 16:15
  • in this array/object i store all the generated data – Ionut Flavius Pogacian Jul 28 '13 at 16:17
  • and what is the result? what is count? – basilikum Jul 28 '13 at 16:17
  • the total data generated is the same as the unique data generated, wich is impossible, at 1 million generated data – Ionut Flavius Pogacian Jul 28 '13 at 16:19
  • 1
    please consider how little of your code I know. But check this out: http://jsfiddle.net/V7drS/ – basilikum Jul 28 '13 at 16:21
  • Yep, looks ok to me. And it will indeed lead to `variante_unice_total` being equal to the length of `variante_unice`. Is that what you were talking about? What value would you expect? – basilikum Jul 28 '13 at 16:29
  • Then you probably want the sum of the counter of every item in your object. So far you are only counting the items you put in your object but you are not adding up the numbers you assign to each item. – basilikum Jul 28 '13 at 16:33
  • yes i do, i count them in `variante`, right where i set the interval – Ionut Flavius Pogacian Jul 28 '13 at 16:34
  • each time i generate a data, i increment the `variante` wich is the total generated data, unique or not – Ionut Flavius Pogacian Jul 28 '13 at 16:35
  • So right now you are incrementing `variante_unice_total` every time a complete new item will be added to your object. But you probably want to increment it also if the counter of one item will be incremented. So maybe you just need to move the line `variante_unice_total++;` out of the `if..else` construct? – basilikum Jul 28 '13 at 17:01
  • no, i spotted the mistake; for me, the combination 1-2-3-4-5-6 is the same as 6-5-4-3-2-1, but as a key, is different; so i get the same combination, but the numbers are switched ... that's why i don't get unique combinations – Ionut Flavius Pogacian Jul 28 '13 at 17:06
  • Because you didn't program it that way. sorry, I know it sounds like a supid answer but it's really the one I can give you. Look at your code and try to figure out what it does. That the string `1-2-3-4-5-6` is not the same as `6-1-2-3-4-5` in JavaScript is not at all suprising. So one last hint: you probably need to sort your random numbers before creating the dash-separated string out of them. – basilikum Jul 28 '13 at 17:28
  • all is ok now ... 10x for the help – Ionut Flavius Pogacian Jul 28 '13 at 20:16