0

I have a job to refractor strings to start using json so they can just pass json objects. So I have made array of names and then I'm trying to go through and make key and values but I'm getting an error in the console that it cant find x of no value. Can someone point me in the right direction?

 var newName = ['ManagingOrg', 'ActiveOrg', 'Severity', 'SeverityClassification', 'WorkQueue', 'TicketState',................ to long to post];


            $().each(newName, function (key, value) {
                key = newName[this];
                value = newValues[this] = $('#' + key).val();
                newArray = [key][value];
                newArray = JSON.stringify(newArray);
                alert(newArray);
                $('.results').html(origArray[TicketNumber]);

            });
6
  • Is there an extra comma at the end of the newName array? Commented Feb 28, 2016 at 0:43
  • nope! I know perl needs the stuff but I tried to dumb it down because the actual code would need to look like this in the end. value = newValues[this] = $('#' + key).val(); I could just make json but its like 100 plus fields and there needs to be 4 so I thought this would be easy and dynamic for future edits. Commented Feb 28, 2016 at 0:53
  • What exactly states the error in your console? And at which line? Commented Feb 28, 2016 at 0:57
  • Unable to get property 'ManagingOrg' of undefined or null reference Commented Feb 28, 2016 at 1:06
  • onedrive.live.com/… Commented Feb 28, 2016 at 1:06

2 Answers 2

1

I'm assuming you have "newValues" and "origArray" defined elsewhere?

In any case you'll need to at least adjust the following:

  • "$().each" should be $.each
  • "newArray" should be defined outside and you should use newArray[key] = value
  • you don't have a variable "TicketNumber" defined and so you should wrap "TicketNumber" in quotes
  • this is a reserved word so you shouldn't use it in "newName[this]" or "newValues[this]"

I suggest using a for loop instead of $.each() based on what you're trying to do inside.

https://msdn.microsoft.com/en-us/library/bb299886.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

I do like your suggestions I do have one question. If I initialize newArray[key] = value that means I would need to create the key and value and would that not create the json or just an array?
Actually, defining "newArray" as an object, var newArray = {}, prior to the loop will give you an object that you can stringify after your loop.
0
var origArray = [];

    var newName = ['ManagingOrg', 'ActiveOrg', 'Severity', 'SeverityClassification'
    ];


    for (var i = 0; i < newName.length - 1; i++) {
        var object = {};
        object[newName[i]] = newName[i];
        object = JSON.stringify(object);
        origArray.push(object);

    }

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.