I am trying to create a json object in AngularJS
My code is as below
for(i=0;i<5;i++){
var dCol = file.columns[i].value;
var dRow = file.rows[i].value;
$scope.fileContents.push({ dCol: dRow });
}
But the value of dCol is not reflecting. It is taking dCol as a string "dCol".
Can someone please help me out. I am fairly new to angularJS. I would really appreciate your help.
Instead of doing this:
$scope.fileContents.push({ dCol: dRow });
please do this:
var tmpObj = {};
tmpObj[dCol] = dRow;
$scope.fileContents.push(tmpObj);
Are youa bit confused with the differences between variables and object properties,
in your example you are adding a property named dCol to the object pushed into fileContents...
by the way,
if the dCol variable is a string you can do something like that:
var item = {}; item[dcol] = dRow; fileContents.push(item);
if the variable dCol isn't a string, you may consider to add it to the object:
fileContents.push({ "dCol": dCol, "dRow": dRow });
that's because dCol is a key and a key is always a string in a JS object;
try this:
for(i=0;i<5;i++){
var obj ={};
var dCol = file.columns[i].value;
var dRow = file.rows[i].value;
obj[dCol]=dRow;
$scope.fileContents.push(obj);
}
if you like a more synthetic approach , this should work to:
$scope.fileContents.push({ [dCol]: dRow });
Related
I'm using Node-Red and the data is passed using JSON objects.
All of the data is in msg.payload. I want to add a new property, the TimeStamp, to the object without all of this unnecessary code...It works but I know this is sloppy.
Is there a better way?
var TimeStamp = new Date();
var newMsg = [ ];
newMsg.push({payload:
{ TimeStamp:TimeStamp ,
Humidity: msg.payload.Humidity,
Temperature: msg.payload.Temperature,
CO2: msg.payload.CO2,
Light: msg.payload.Light
}
});
return newMsg;
You can add the new property to the existing msg object and pass it on.
msg.payload.TimeStamp = new Date();
return msg;
This is the better approach as it leaves all other message properties untouched.
I have a string and i would like to pass it by reference in a object.
var str:String = '';
var object:Object = {s:str};
object.s = 'test';
trace(str); // i would like this to output test but it is just blank
trace(object.s); //this has a value stored in it
Is this possible or am i thinking about this wrong?
What i am trying to do is update the str value by updating object.s. I want the sting object to be inside the object not the value of the str.
I want it to do something more like this
var obj2:Object = new Object();
obj2.txt = '';
var object:Object = {s:obj2};
object.s.txt = 'test';
trace(obj2.txt); // outputs test
trace(object.s.txt); //outputs test
Of course it is possible. In the above example i suspect the trace works fine, but since there is no value, it does not display anything. I don't know actionscript very well, but since it compiles down to javascript then all objects are passed by reference. your example code actually has two objects in it one called string that is a string with no value assigned, and the other an object with a property called s that is a string. that it why there is no output.
var string:String = '';
var object:Object = {s:string};
object.s = 'test';
string = object.s;
trace(string); // i would like this to output test but it is just blank
trace(object.s);
This should show you what you want to see, and is passed by reference, unless actionscript enforces pass by value, which i do not believe it does
I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.
Instead of many if conditionals, I want to call a constructor according to a string value
var valueString:String = "myNewClassB";
var value:Class = valueString as Class;
new value() // new value() == new myNewClassB()
I know it's gonna fail, I need help. Thanks.
var ClassReference:Class = getDefinitionByName("myNewClassB");
var instance = new ClassReference();
That's the basics, bud.
If you want to do that, there are two ways, either assign classes to a list of classes made for an example in a object:
var list:Object = {
classA: FirstClass,
classB: SecondClass,
classC: ThirdClass
}
and than call them by a string:
var desiredObject:* = new (list["classA"] as Class)();
or you could also use getDefinitionBtName but than if you want to get a class you need to provide a full name (with the package)
var desiredClass = getDefinitionByName( "com.somedomain.SomeClass" );
If you are laoding an SWF content and than want to get a class from it you should use that loader loaderInfo.applicationDomain.getDefinition( "....class" );
you can also check if a class is defined by:
loaderInfo.applicationDomain.hasDefinition( "....class" );
link: ApplicationDomain.getDefinition
link: ApplicationDomain.hasDefinition
link: LoaderInfo
How to send array in Httpservice in Adobe Flex3
I am not quite sure what you mean by sending an array to a httpservice. If you mean to send an array to a httpservice with the same field name, you can pass an array as field value.
var service:HTTPService = new HTTPService();
service.useProxy = true;
service.destination = "myservicet";
service.resultFormat = HTTPService.RESULT_FORMAT_XML;
var fields:Array = ["categories", "organisation"];
var params:Object = new Object();
params.q = "stackoverflow";
params.rows = 0;
params.facet = "true";
params["facet.field"] = fields;
service.send(params);
The HTTPService will convert this to the url parameters:
facet=true&q=stackoverflow&facet%2Efield=categories&facet%2Efield=organisation&rows=0
Hope this helps!
Added for more clarity. When there is only 1 argument in the array, do not pass the fields as an array. For some reason, flex will not send this to the http service
It really depends what is the back end technology you're using. If you're sending it to PHP you could try:
var fields:Array = ["categories", "organisation"];
var params:Object = {};
params.q = "stackoverflow";
params.rows = 0;
params.facet = "true";
params["facet.field[]"] = fields;
service.send(params);
PHP will generate an array for you.
AFAIR this works fine in Rails as well.
if it is a simple string array, you can join it with a well know separator char, and on the other site, split the string with the same separator back to an array.
If it is a simple array, you could send it as a comma separated string.
httpService.request = new Object;
httpService.request.csv = array.toString();