0

I'd like to create that function :

generateJson = function()
{
    var controls = {
        "invoicedate" : "invoicedate",
        "duedate" : "duedate"
    };

    var jsonLog = {};

    $.each(controls, function(index, value, jsonLog) {
      jsonLog[value] = "t";
    });
    $('#jsoncode').val(JSON.stringify(jsonLog));
}

I have an error : Uncaught TypeError: Cannot set property 'invoicedate' of undefined

How can I make the jsonLog var available in my inline function ?

Regards

2 Answers 2

3

Remove the third argument from the function you pass to each since:

  1. The each won't pass anything into it
  2. It masks the variable of the same name in the wider scope that you assigned the object to
Sign up to request clarification or add additional context in comments.

1 Comment

My dear. you are right. I add it to test it and I forgot to remove it .. sorry for disturbing, it's rly obvious
2

passing jsonLog as third argument is overriding or masking the previous actual jsonLog in parent scope

$.each(controls, function(index, value) {
      jsonLog[value] = "t";
    });

Comments

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.