I need to dynamically generate a object like this
{type:"typeA",size28:0,size29:0,size30:0 etc...}
I get the sizes from a xml file as an array and I need to insert it like this
{type:"typeA",here the generated size array but as the object properties}
How can I do this?
Thanks in advance.
I assume that the length of the array is variable:
var obj : Object = {type:"typeA"}
for (var i : int = 0; i < generatedArray.length; i++) {
obj['size'+(28+i)] = generatedArray[i];
}
var xmlData:XML = <obj>
<contents>size28:5,size29:3,size30:9</contents>
</obj>;
var obj:Object = new Object();
var xmlObjArray:Array = xmlData.contents.split(",");
for (var i in xmlObjArray)
{
var objProp:Array = xmlObjArray[i].split(":");
obj[objProp[0]] = objProp[1];
}
trace (obj.size29);
Related
I want to print class name such as Insect,Insect2 from an array. but I don't do this. I am also try using insects[i].name. But the output will shown only object. But I want to show Insect,Insect2. How can I solve this.
private var insects:Array;
var ins:MovieClip;
var ins:MovieClip;
//trace(Math.random());
if(Math.random()>0.5){
ins = new Insect();
ins.speedY = insectSpeed;
ins.score = -10;
}else{
ins = new Insect2();
ins.speedY = insect2Speed;
ins.score = 1;
}
game_mc.addChild(ins);
insects.push(ins);
for(var i:int=0;i<insects.length;i++){
trace(insects[i].name);
trace(insects[i]);
}
Output---1
I want to print only Insect or Insect2 name. How can I do that?
There's a method to obtain a class name from the object or class reference.
import flash.utils.getQualifiedClassName;
for (var i:int = 0; i < insects.length; i++)
{
trace(getQualifiedClassName(insects[i]));
}
I have an ArrayCollection of a list of usernames and user id's. In this list there are duplicates that I need to remove. I've searched the internet and while there are a lot of example of this using Arrays, I can't find any clear examples using ArrayCollection's.
The should be simpler then the other solution.
function removeDuplicatesInArray(val:*, index:uint, array:Array):Boolean {
return array.indexOf(val) == array.lastIndexOf(val);
}
function removeDuplicatesInCollection(collection:ArrayCollection):ArrayCollection {
collection.source = collection.source.filter(removeDuplicatesInArray);
return collection;
}
Here's what I found after quick googling.
//takes an AC and the filters out all duplicate entries
public function getUniqueValues (collection : ArrayCollection) : ArrayCollection {
var length : Number = collection.length;
var dic : Dictionary = new Dictionary();
//this should be whatever type of object you have inside your AC
var value : Object;
for(var i : int= 0; i < length; i++){
value = collection.getItemAt(i);
dic[value] = value;
}
//this bit goes through the dictionary and puts data into a new AC
var unique = new ArrayCollection();
for(var prop:String in dic){
unique.addItem(dic[prop]);
}
return unique;
}
If you find solutions for the array you can do the same with the ArrayCollection. You can change arrayCollection.source and arrayCollection will be changed too. In general, we can assume that ArrayCollection is wrapper for Array.
Array contain a filter function and we can make use of it as following.
var ar:Array = ["Joe","Bob","Curl","Curl"];
var distinctData = ar.filter(function(itm, i){
return ar.indexOf(itm)== i;
});
Alert.show(distinctData.join(","));
Or better yet
Array.prototype.distinct = function():*
{
var arr:Array = this as Array;
return arr.filter(function(itm, i){
return (this as Array).indexOf(itm)== i;
},arr);
};
var ar:Array = ["Joe","Bob","Curl","Curl"];
Alert.show(ar.distinct());
function removeDuplicateElement(_arr:Array):Array{
//set new Dictionary
var lDic:Dictionary = new Dictionary();
for each(var thisElement:* in _arr){
//All values of duplicate entries will be overwritten
lDic[thisElement] = true;
}
_arr = [];
for(var lKey:* in lDic){
_arr.push(lKey);
}
return _arr;
}
i am trying to return the Value of the Objects name, any help would be a huge help! thank you.
var o:Object = new Object();
var n:String = "NAME"
o[n] = "DATA";
for each (var p in o){
trace("name="+o[p]+" data="+p);
}
outputs -
name=undefined data=DATA
where it should be outputting -
name=NAME data=DATA
For looping thru Object properties, drop the "each":
var o:Object = new Object();
var n:String = "NAME"
o[n] = "DATA";
for (var p in o)
{
trace("name="+o[p]+" data="+p);
}
I believe you want to use a regular for loop vs a "for each" loop http://active.tutsplus.com/tutorials/actionscript/as3-101-loops/
I am trying to create a vector from unknown class, but it fails, any ideas about how to do it?
This is what i tried:
var vector:Vector = new Vector(); // throw exception
function base():void{
var vector:Vector.<String> = createVector(String);// throw classCastException
}
function createVector(cls:Class):*{
var array:Array = new Array();
for(var i:int = 0; i < 10; i++){
var element:cls = new cls();
array.push(element);
}
return Vector(array);
}
Vector is expecting a parameter type so you can't do this like you want, but using getQualifiedClassName to get class info you can construct a string that will enable you to call the Vector. constructor using getDefinitionByName :
Ex.
// get class parameter name
var className:String=getQualifiedClassName(String);
// get the Vector class object for the given class
var o:Object=getDefinitionByName("__AS3__.vec::Vector<"+className+">");
// call the constructor
var v:*=o.prototype.constructor(["hello", "world"]);
So your function can be written as:
public function createVector(cls:Class):*{
var cn:String = getQualifiedClassName(cls);
var o:Object = getDefinitionByName("__AS3__.vec::Vector.<"+cn+">");
var array:Array = [];
for(var i:int = 0; i < 10; i++){
var element:* = new cls();
array.push(element);
}
return o.prototype.constructor(array);
}
live example at wonderfl:
http://wonderfl.net/c/pkjs
Based on #Patrick answer I found a working solution.
Check it out:
function createVector(cls:Class):*{
var className:String = getQualifiedClassName(cls);
var vectorClass:Class = getDefinitionByName("__AS3__.vec::Vector.<"+className+">") as Class;
var vector:* = new vectorClass(10);
for(var i:int = 0; i < 10; i++){
var element:MyClass = new cls(); // may be Object or Object extends cls
vector[i] = element;
}
return vector;
}
In AS3, how do i sort this list alphabetically? I'm not a programming expert and i've had a hard time just figuring this out.
var searchObject:Object = new Object();
var mapXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("interactive-map.xml"));
xmlLoader.addEventListener(Event.COMPLETE, parseXML);
function parseXML(event:Event):void {
mapXML = new XML(event.target.data);
for each (var locationElement:XML in mapXML.maplocation) {
searchObject.locationname = locationElement.attribute("locationname");
searchObject.category = locationElement.attribute("category");
trace("Location Name: " + searchObject.locationname);
}
}
first you need to push elements to array/vector.
Then you can sort array with sortOn or sort methods.
Check sortOn/sort methods reference to find sorting options.
var searchObject:Object = new Object();
var searchObjectArray:Array = new Array
var mapXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("interactive-map.xml"));
xmlLoader.addEventListener(Event.COMPLETE, parseXML);
function parseXML(event:Event):void {
mapXML = new XML(event.target.data);
for each (var locationElement:XML in mapXML.maplocation) {
searchObject = new Object();
searchObject.locationname = locationElement.attribute("locationname");
searchObject.category = locationElement.attribute("category");
searchObjectArray.push(searchObject);//pushing elements to array
}
searchObjectArray.sortOn("locationname")//sorting array
for each ( var searchObjectElement:Object in searchObjectArray)
{
trace("Location name: " + searchObjectElement.locationname);//printing sorted array
}
}
Try something ike this:
var nodes:Array = [];
for each (var locationElement:XML in mapXML.maplocation) {
nodes[nodes.length] = locationElement;
}
nodes.sortOn(locationname);
I'm not sure if this will work with XML array elements. If it doesn't, try using sort() with a compareFunction.
I think you need to somehow wrap this in a collection class or map it over like Amy suggests, here's an example that uses an XMLCollection http://www.webdevotion.be/blog/2008/01/22/how-to-sort-an-xmllist-using-e4x/