Kevin Lindmark
Kevin Lindmark

Reputation: 1257

function for array returns null

The below function returns this output. But I can't understand why. Any clues? Output: {"A":{"antal":null},"B":{"antal":null},"C":{"antal":null},"D":{"antal":null},"E":{"antal":null},"G":{"antal":null}}

Function is,

    function seriestat(){
        var statserier = {};

        $.each(globalSIEdata["#EXTRA"]["VERSERIER"], function(i, item) {
                    statserier[i] = {};
        });
        $.each(globalSIEdata["#VER"], function(i2, item2) {
                var serie = i2.substring(0, i2.indexOf('-')); 
                statserier[serie]["antal"] += 1;
        });
        return statserier; 
    }

Here is example from globalSIEdata:

{    "#VER": {
    "A-1": {
        "verdatum": "2017-01-03"
    },
    "A-2": {
        "verdatum": "2017-01-03"
    },
    "B-1": {
        "verdatum": "2017-01-03"
    },
    "B-2": {
        "verdatum": "2017-01-03"
    }
    "A-3": {
        "verdatum": "2017-01-03"
    }

}

Upvotes: 0

Views: 69

Answers (1)

justadudewhohacks
justadudewhohacks

Reputation: 681

You forgot to initialize the "antal" property thus it will be undefined, try something like:

statserier[serie]["antal"] = (statserier[serie]["antal"] || 0) + 1;

Alternatively you could try to initialize your statserier object as follows instead:

statserier[i] = { antal: 0 };

Upvotes: 3

Related Questions