0

I have a variable called jsonAllSignOffs that is created by a .NET JSON service and sent to the client. It is essentially a struct containing various arrays. The values of these arrays are arranged such that if you took the nth element of each array, all together that would be the collected properties of the nth Sign Off. Obviously a list of Sign Off objects containing these properties would be better, but unfortunately this is a legacy application and changing it's architecture in this manner is out of scope.

What I'm trying to do is create a variable called jsonUserSignOffs that is essentially a subset of jsonAllSignOffs with all the same properties. However jsonAllSignOffs is not a type that I can instantiate. I figured declaring a variable and assuming the properties by assigning into them would "build" the object, but apparently that's not the case.

var jsonUserSignOffs;
jsonUserSignOffs.isAuthor = jsonAllSignOffs.isAuthor; //error jsonUserSignOffs is undefined

Since javascript doesn't support classes and is pretty lax with variables I figured the only way to create a struct like jsonAllSignOffs was to declare the variable and assign values to it's properties. I know these properties are not defined anywhere, but I thought assigning values to them would instantiate them at the same time. I come from a C# background where I would use a class. Javascript is less familiar to me, and I'm unclear on how to proceed.

5
  • 6
    var jsonUserSignOffs; -> var jsonUserSignOffs = {}; Commented Apr 16, 2013 at 20:32
  • 3
    Make the variable an empty object before assigning properties. var jsonUserSignOffs={}; Commented Apr 16, 2013 at 20:33
  • Lets see the structure of the jsonAllSignOff object. Commented Apr 16, 2013 at 20:33
  • Rob W: That worked. josh3736: Thank you as well. If one of you would care to make it an answer, I'll mark it as correct. Commented Apr 16, 2013 at 21:11
  • As this question currently stands, it won't be terribly useful to others. The answer is basically an excerpt from "JavaScript 101: Objects" and the question is very localized. Hopefully people who have the same question as you did will stumble onto this answer... (or one of the many similar question/answers on this site). Commented Apr 16, 2013 at 21:19

1 Answer 1

5

Try this

var jsonUserSignOffs = {}; //creates an empty object using object literal notation
jsonUserSignOffs.isAuthor = jsonAllSignOffs.isAuthor;

OR:

var jsonUserSignOffs = {
    isAuthor: jsonAllSignOffs.isAuthor
};
Sign up to request clarification or add additional context in comments.

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.