0

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"
    }

}

7
  • 1
    You don't understand why a function that creates and populates an object returns an object? Commented Oct 7, 2017 at 15:47
  • 1
    what does globalSIEdata looks like? can you share some sample/minimal data for review/testing? Commented Oct 7, 2017 at 15:51
  • 1
    What is expected result of statserier[serie]["antal"] += 1? Commented Oct 7, 2017 at 15:51
  • 1
    You got to be setting null somewhere else, there's no way += 1 would ever get you null Commented Oct 7, 2017 at 15:54
  • Added example from globalSIEdata above. Want to count the number of lines per object where item begins with A, B, C etc. Commented Oct 7, 2017 at 15:59

1 Answer 1

3

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 };
Sign up to request clarification or add additional context in comments.

2 Comments

undefined + 1 would get you NaN and he's getting null
Well the output he posted is stringified, and NaN stringified is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.