I have a json object like this:
- var wDefaults = {
"testPortalA":[
{"wid":"w001","wclosed":false,"wcollapsed":true,"wmaximized":false,"pcol":0,"wpos":0},
{"wid":"w002","wclosed":false,"wcollapsed":true,"wmaximized":false,"pcol":1,"wpos":0},
{"wid":"w003","wclosed":false,"wcollapsed":true,"wmaximized":false,"pcol":2,"wpos":0}
],
"testPortalB":[
{"wid":"w004","wclosed":false,"wcollapsed":true,"wmaximized":false,"pcol":0,"wpos":0},
{"wid":"w005","wclosed":false,"wcollapsed":true,"wmaximized":false,"pcol":1,"wpos":0},
{"wid":"w006","wclosed":false,"wcollapsed":true,"wmaximized":false,"pcol":2,"wpos":0}
]
}
I understand how to get or set a value
if I know its position in the array:
- myval = wDefaults.testPortalA[0].wid; //get a value
- wDefaults.testPortalA[0].wclosed = true; //set a value
I understand how to access
all of the values using .each():
- $(wDefaults["testPortalA"]).each(function() { //for the "testPortalA" array in the "wDefaults" object
$(this).each(function(index, value) { //for each object in that array
myval = $(this)[index]["wid"]; //get a value
$(this)[index]["wclosed"] = true; //set a value
});
});
Now, here's where I have questions:
(A) Is there any way to access a specific value in an object if I
don't know its index in the array, other than looping through
all objects? For example, if I want to get or set the wclosed value for an object in testPortalA where its wid = "w002".
(B) Is it possible to use variables in getting/setting values? For example, can I do something like this:
- var whichPortal = "testPortalA";
- myval = wDefaults.whichPortal[0].wid;
(C) Can I save more than one type of object in an array of objects? For example, in the object above, it is saving a "wmaximized" value in each object. At any given time, only one object in the array will have a 'true' value. It seems to me that I should really only be saving one object: "wmaximized":"w001", where its value is the wid of whatever object is currently maximized. Then I would have the following object, which would probably screw up my looping through all objects, so probably isn't the way to do it. Can anyone tell me how to do this the right way?
- var wDefaults = {
"testPortalA":[
{"wmaximized":"w002"},
{"wid":"w001","wclosed":false,"wcollapsed":true,"pcol":0,"wpos":0},
{"wid":"w002","wclosed":false,"wcollapsed":true,"pcol":1,"wpos":0},
{"wid":"w003","wclosed":false,"wcollapsed":true,"pcol":2,"wpos":0}
],
"testPortalB":[
{"wmaximized":"w005"},
{"wid":"w004","wclosed":false,"wcollapsed":true,"pcol":0,"wpos":0},
{"wid":"w005","wclosed":false,"wcollapsed":true,"pcol":1,"wpos":0},
{"wid":"w006","wclosed":false,"wcollapsed":true,"pcol":2,"wpos":0}
]
}
I would greatly appreciate any help anyone can give me!