Making comparisson to items in an Object - actionscript-3

I have the following object:
var users:Object= new Object();
users[0]["user_id"] = "1124";
users[0]["name"] = "ikke";
users[0]["age"] = "24";
users[0]["gender"] = "male";
users[1]["user_id"] = "1318";
users[1]["name"] = "test";
users[1]["age"] = "20";
users[1]["gender"] = "male";
var selectors:Object = new Object();
selectors["user_id"] = 1318;
selectors["gender"] = "male";
what i want is to use the selectors object in an if statement. In humans lanuguage it should be something like:
for (var index:String in users) {
If users[index]["gender"] == selectors[gender] && users[index]["user_id"] == "male" -> then trace "success".
}
The tricky part is that the selectors object is dynamic. Sometimes it can contain only 1 item , sometimes 3 items. Or it can also be null. In that case it should allways trace success. Anyone that can help me?

for(var i:int = 0; i < users.length; i++) {
var success:Boolean = true;
for(var key:String in selectors) {
if(users[i][key] != selectors[key]) {
success = false;
break;
}
}
if(success) {
trace('success for user ' + i);
}
}

Related

Retrieving google form response data

I have a google form, where I need to use item ID and get all the response for that Item.
I have the below script which will timeout if the form has more than 3000 responses, as its inefficient
How Do I optimize it to retrieve all the items in a short span of time
fO.items = ["ItemID1","ItemID2","ItemID3"...];
for (var i = 0; i < responses.length; i++) {
var response = responses[i];
var otherItems = '';
var flag = true;
for (var j = 0; j < fO.items.length; j++) {
var item = form.getItemById(parseInt(fO.items[j]));
if (response.getResponseForItem(item))
var otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
else
flag = false;
}
if (flag) {
columnData.push(otherItems);
responseIds.push(response.getId());
}
}
Currently, your code is getting the item object with the following line:
var item = form.getItemById(parseInt(fO.items[j]));
So, that line of code is reading the Form many times.
You could try getting the item objects once, putting them into a JSON object, and then retrieving them as needed.
I haven't tested this code, and I don't know if it will work, or if it will be faster if it does work. But thought I'd share the idea.
function getSomeAnswers() {
var form,flag,i,item,itemID,itemList,itemsObject,
k,L,L_items,otherItems,responses,response,thisAnswer;
itemList = ["ItemID1","ItemID2","ItemID3"];
form = FormApp.getActiveForm();
responses = FormApp.getActiveForm().getResponses();
itemsObject = {};
L_items = itemList.length;
for (i = 0; i < L; i++) {//Compile a list of item objects
itemID = parseInt(itemList[i]);
itemsObject[itemID] = form.getItemById(itemID);
}
L = responses.length;
for (i = 0; i < L; i++) {
response = responses[i];
otherItems = '';
flag = true;
for (k in itemsObject) {//Loop through every item to get
item = itemsObject[k];
thisAnswer = response.getResponseForItem(item);
Logger.log(thisAnswer)
if (thisAnswer)
otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
else
flag = false;
}
/*
if (flag) {
columnData.push(otherItems);
responseIds.push(response.getId());
}
*/
}
}

how to make ordered and unordered list in actionscript 3?

How to create simple ordered list in actionscript 3?
like
aaaa
bbbb
cccc
I have to take the text from TextArea.
UPDATE:
private function applyStyleEvent(event:Event):void
{
inputText = new TextLayoutFormat(inputTextArea.getFormatOfRange(null,startIndex,endIndex));
if(event.currentTarget.id == "boldBttn")
{
inputText.fontWeight = (inputText.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD;
}
else if(event.currentTarget.id == "italicBttn" )
{
inputText.fontStyle = (inputText.fontStyle == FontPosture.ITALIC) ? FontPosture.NORMAL : FontPosture.ITALIC;
}
else if(event.currentTarget.id == "underLineBttn" )
{
inputText.textDecoration =(inputText.textDecoration == TextDecoration.UNDERLINE)?TextDecoration.NONE : TextDecoration.UNDERLINE;
}
}
Considering that you have an array with your content, you can try:
var textArea = new TextArea();
addChild(textArea);
var list:Array = ["ccccc", "bbbbb", "aaaaa", "ddddd"];
list.sort(Array.CASEINSENSITIVE); // default sort
trace(list); // output: aaaaa,bbbbb,ccccc,ddddd
for(var i:int = 0, l:uint = list.length; i < l; i++)
{
textArea.appendText(list[i] + '\n');
}
It's a way to sort your list, you can adapt to your code and make all necessary changes.
You should have a look at this :
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fa4.html
Edit :
If you want to use bullets for TextField objects, you need to apply a bulleted TextFormat :
var tf:TextField = new TextField();
var bulletTF:TextFormat = new TextFormat();
bulletTF.bullet = true;
tf.setTextFormat(bulletTF);
If you need to use it inside your TextArea, use HTML :
var str:String = "<p>Here is your list :</p>";
for(var i:int = 0, i < yourList.length; i++)
{
str += "<li>" + yourList[i] + "</li>";
}
myTextArea.htmlText += str;

dynamically add children to component, then dynamically manipulate them?

i dont know why this doesnt work. i have a tabnavigator which i dynamically add a navigatecontainer to which has a textarea dynamically added to the nav container. the id's of both have the same stringname as the chatguys[c][0].
it gives errors saying 'TypeError: Error #1034: Type Coercion failed: cannot convert spark.skins.spark::SkinnableContainerSkin#9737851 to spark.components.TextArea." at runtime. wut do?
any help would be greatly appreciated, thank you :(
var idx:uint;
const len:uint = navigate.numChildren;
var alreadyexists:Boolean = false;
for (idx = 0; idx < len; idx++) {
//var check:spark.components.TextArea = navigate.getElementAt(idx) as spark.components.TextArea;
//var check:Tab = navigate.getTabAt(idx) as Tab;
var check:NavigatorContent = navigate.getChildAt(idx) as NavigatorContent;
// var check = navigate.getElementAt(idx);
//elmt.selected = tgBtn.selected;
if (check.label == evt.username)
{// trace(navigate.getChildAt(idx).label);
var c:uint;
const p:uint = chatguys.length;
for (c = 0; c < p; c++){
if(chatguys[c][0] == evt.username){
spark.components.TextArea(DisplayObjectContainer(navigate.getChildAt(idx)).getChildAt(chatguys[c][0])).textFlow=TextConverter.importToFlow(chatguys[c][1], TextConverter.TEXT_FIELD_HTML_FORMAT);
}}
b here ill show you the second half which works fine, but maybe i need to do it differently to get waht i need done ?
var chats:Array = [];
var chatguys:Array = [];
public function userlist_click() :void{
var windowname:Object =users_lst.selectedItem;
var idx:uint;
const len:uint = navigate.numChildren;
trace(navigate.numChildren);
var alreadyexists:Boolean = false;
for (idx = 0; idx < len; idx++) {
var check:NavigatorContent = navigate.getChildAt(idx) as NavigatorContent;
if (check.label == windowname.name)
{
alreadyexists = true;
}
}
if (alreadyexists == false)
{
chats[windowname.name] = new spark.components.TextArea();
chats[windowname.name].x = 10;
chats[windowname.name].y= 32;
chats[windowname.name].width= 517.19696 ;
chats[windowname.name].height= 343.18182;
chats[windowname.name].scroller
chats[windowname.name].x="9";
chats[windowname.name].y="2";
chats[windowname.name].width="517.19696";
chats[windowname.name].height="343.18182";
chats[windowname.name].setStyle("skinClass", spark.skins.spark.TextAreaSkin);
//textArea.skinClass= "spark.skins.spark.TextAreaSkin";
chats[windowname.name].text="ffg";
chats[windowname.name].setStyle("verticalScrollPolicy", ScrollPolicy.ON);
var match = new Array();
match.push(windowname.name);
match.push('bob');
chatguys.push(match);
for (var i in chatguys){
if (chatguys[i][0] == windowname.name){
chats[windowname.name].textFlow=TextConverter.importToFlow(chatguys[i][1], TextConverter.TEXT_FIELD_HTML_FORMAT);
}}
chats[windowname.name].id = windowname.name;
trace(chats[windowname.name]);
var messagebox:NavigatorContent = new NavigatorContent;
messagebox.percentWidth= 100;
messagebox.percentHeight= 100;
messagebox.label = chats[windowname.name];
messagebox.id = chats[windowname.name];
trace(messagebox.label);
messagebox.addElement(chats[windowname.name]);
navigate.addChild(messagebox); }}

setting the chances of occurrence of elements in an array

For example I have 3 elements in an array:
public function randomTile():Number
{
var tiles:Array = new Array(fire,ice,water);
var index:Number=Math.floor(Math.random()*tiles.length);
return tiles[index];
}
How to set the chances of occurrence of fire(70%), ice(10%), and water(20%)?
This should work for any number of elements and you can specify any chance value.
var tiles:Array = [
{"item":"fire", "chance":70 },
{"item":"ice", "chance":10 },
{"item":"water","chance":20}
];
var picked:Object = pickRandomByChance(tiles);
trace(picked.item);
public function pickRandomByChance(options:Array):Object
{
var copy:Array = [];
var range:Number = 0;
for (var i:int = 0; i < options.length; i++)
{
copy.push( { "item":options[i].item, "chance":options[i].chance } );
range += copy[i].chance;
if (i > 0)
copy[i].chance += copy[i - 1].chance;
}
var pick:Number = Math.floor(Math.random() * range);
for (i = 0; i < copy.length; i++)
{
if (pick <= copy[i].chance)
return copy[i];
}
return null;
}
There quite a few ways you could do this, and it largely depends of the scope of your project. If you just have the three elements, using a switch statement would be easy:
var rand:Number = Math.random();
switch(true){
case rand >= .3:
//use fire
break;
case rand >= .1
//use water
break;
default:
//use ice
}
Someone else may have a better way though

XMLListCollection find index value

I want to find specific value on a XMLListCollection.
I try to use something like this but it doesn't work!
var xmllisteRDV:XMLList= XML(event.result).RDVClinik;
xmlCollSuivi = new XMLListCollection(xmllisteRDV);
var index:Number = -1;
for(var i:Number = 0; i < xmllisteRDV.length(); i++)
{
if(XML(xmllisteRDV[i]).#grDateDeb == todayDate)
{
index = i;
break;
}
}
First going to try pointing out errors in the original code:
var xmllisteRDV:XMLList= XML(event.result).RDVClinik; //Unnecessary cast, event.result is Object compiler will not check or know the run-time type, doesn't care because Object is declared dynamic meaning properties can be added to it dynamically, if RDVClinik didn't exist on the particular Object type it would simply be null casting as XML gives it no information about this "property"
xmlCollSuivi = new XMLListCollection(xmllisteRDV);
var index:Number = -1;
for(var i:Number = 0; i < xmllisteRDV.length(); i++) //length is a property not a method on XMLListCollection this should throw a compile time error
{
if(XML(xmllisteRDV[i]).#grDateDeb == todayDate)// I see no type when debugging for the result of xmllisteRDV[i] not positive here but this cast is at the least unnecessary
{
index = i;
break;
}
}
Here's a version I think will work possibly with changes to how todayDate is built
var date:Date = new Date();
var todayDate:String = date.dateUTC+"/"+date.dayUTC+"/"+date.fullYear;
var index:int=-1;
for(var i:int = 0; i < flex3Projects.length; i++)
{
trace(xmllisteRDV[i].#grDateDeb)
if(xmllisteRDV[i].#grDateDeb.toString() == todayDate)
{
index = i;
break;
}
}
With you help, I found the solution
private function setSelectedItem():void
{
var gData:Object = dgSuiviClini.dataProvider;
var todayDate:String= new DateUtility().DateAsToString(new Date());
for(var i:Number=0; i < gData.length; i++)
{
var thisObj:Object = gData.getItemAt(i);
if(thisObj.grDateDeb == todayDate)
{
dgSuiviClini.selectedIndex = i;
//sometimes scrollToIndex doesnt work if validateNow() not done
dgSuiviClini.validateNow();
//dgSuiviClini.scrollToIndex(i);
}
else{
dgSuiviClini.validateNow();
// dgSuiviClini.scrollToIndex(gData.length);
}
}
dgSuiviClini.validateNow();
dgSuiviClini.editedItemPosition = { rowIndex: gData.length-1, columnIndex: nColSaisie };
}
Thanks