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.

Legion
  • 3,922
  • 8
  • 51
  • 95
  • 6
    `var jsonUserSignOffs;` -> `var jsonUserSignOffs = {};` – Rob W Apr 16 '13 at 20:32
  • 3
    Make the variable an empty object before assigning properties. `var jsonUserSignOffs={};` – josh3736 Apr 16 '13 at 20:33
  • Lets see the structure of the `jsonAllSignOff` object. – Ryan Apr 16 '13 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. – Legion Apr 16 '13 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](http://stackoverflow.com/a/9362754/778118)... (or one of the many similar question/answers on this site). – jahroy Apr 16 '13 at 21:19

1 Answers1

5

Try this

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

OR:

var jsonUserSignOffs = {
    isAuthor: jsonAllSignOffs.isAuthor
};