Search Multidimensional Array ActionScript 3 - arrays

If I have two arrays like the example below, how can I search the first part of each node in the 'score' array with the values in the 'search' array and return the value that is the second part of each node in the 'score' array? Basically in this case I'd want to get 5 and 7.
var score:Array = new Array();
score[0] = ["cat", "3"];
score[1] = ["dog", "5"];
score[2] = ["fish", "0.5"];
score[3] = ["bird", "0.25"];
score[4] = ["horse", "10"];
score[5] = ["cow", "15"];
score[6] = ["iguana", "7"];
var search:Array = ["dog", "iguana"];

Try with this code (using Dictionary):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var score:Dictionary = new Dictionary();
private var search:Array = ["dog", "iguana"];
public function init():void{
score["cat"] = "3";
score["dog"] = "5";
score["fish"] = "0.5";
score["bird"] = "0.25";
score["horse"] = "10";
score["cow"] = "15";
score["iguana"] = "7";
var t1:String = score[search[1]];
var t2:String = score[search[0]];
Alert.show(t1 + ' ' + t2); //prints 5 7
}
]]>
</mx:Script>
</mx:Application>
Or you can do this too:
var t1:String = score["dog"];
var t2:String = score["iguana"];
Or without Dictionary:
<?xml version="1.0" encoding="utf-8"?>
public function init():void{
score[0] = ["cat","3"];
score[1] = ["dog","5"];
score[2] = ["fish","0.5"];
score[3] = ["bird","0.25"];
score[4] = ["horse","10"];
score[5] = ["cow","15"];
score[6] = ["iguana","7"];
for(var j:int = 0;j<search.length;j++){
for(var i:int = 0;i<score.length;i++){
var temp:Array = score[i] as Array;
if(temp[0] == search[j]){
Alert.show(temp[1]);
}
}
}
}
]]>
</mx:Script>
Also you have not used an appropriate way to declare arrays, I recommend using my method or changing the array declaration.

Related

Pushing Distinct Values to Array and Finding New Length

Trying to sort and push only the distinct values into a new array and then find the length of the resulting array. See code below. The UniqueTeams.length is coming out to be 1, when it should be 5 (I thought).
var teams = [[formSS.getRange("F8").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("F12").getValue(),
formSS.getRange("F14").getValue(),
formSS.getRange("F16").getValue()]];
var uniqueTeams = removeDups(teams);
if(uniqueTeams.length < 5){
{SpreadsheetApp.getUi().alert(uniqueTeams);
return;}
}
function removeDups(array) {
var outArray = [];
array.sort();
outArray.push(array[0]);
for(var n in array){
if(outArray[outArray.length-1]!=array[n]){
outArray.push(array[n]);
}
}
return outArray;
}
uniqueTeams = Alabama (2),Maryland (10),Cleveland St (15),BYU (6),Liberty (13)
uniqueTeams.length = 1
Get unique elements
function remdup() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const t = sh.getRange('F8:F16').getValues().flat();
let teams = [t[0], t[2], t[4], t[6], t[8]];
let s = new Set(teams)
teams = [...s];
Logger.log(teams.join(','))
}
Set
Cooper's answer it the best ultimate solution. But if you want just to fix your code you can try to change the line:
var uniqueTeams = removeDups(teams);
with:
var uniqueTeams = removeDups(teams[0]);
Since the teams is a 2D array [[...]].

Array Map multidimensional, get values from 3 array Java

Let say I have 3 arrays,
compCD = ["909", "908", "080", "901"];
contNO = ["09999", "08888", "00777", "00666"];
pomNum = ["A01", "A02", "A03", "A04"];
How can I insert into jsonMap each separated Values like
[
{
"compCD" = "909",
"contNO" = "09999",
"pomNum" = "A01"
},{
"compCD" = "908",
"contNO" = "08888",
"pomNum" = "A02"
}
]
we can assume that each array size is the same. The first of each array will bi insert first to map.
How we can solve those with minimum loop.
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=[];
for(var i=0; i<compCD.length; i++) {
jsonMap.push(
{compCD:compCD[i], contNO:contNO[i], pomNum:pomNum[i]}
);
};
console.log(jsonMap);
var compCD = ["909", "908", "080", "901"];
var contNO = ["09999", "08888", "00777", "00666"];
var pomNum = ["A01", "A02", "A03", "A04"];
var jsonMap=compCD.map(
(currentValue, index)=>[currentValue, contNO[index], pomNum[index]]
);
console.log(jsonMap);

Action Script 3 URLLoader in for loop

I have 7 Arrays to begin with:
private var listArray:Array = new Array();
private var oneArray:Array = new Array();
private var twoArray:Array = new Array();
private var threeArray:Array = new Array();
private var fourArray:Array = new Array();
private var fiveArray:Array = new Array();
private var sixArray:Array = new Array();
listArray contain 6 string element of text file name.
something like:
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
All other array is empty at the moment.
I have wrote a for loop like this:
for (var i:int = 0; i < listArray.length; i++)
{
var urlRequest:URLRequest = new URLRequest(File.documentsDirectory.resolvePath(listArray[i]).url);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try
{
urlLoader.load(urlRequest);
}catch (error:Error){
trace("Cannot load : " + error.message);
}
}
if without for loop I know I can do this for only one array of data:
private function completeHandler(e:Event):void
{
oneArray = e.target.data.split(/\r\n/);
}
Here I am trying to get something to work like:
oneArray contain the data from 1.txt
twoArray contain the data from 2.txt
so on...
sixArray contain the data from 6.txt
problem:
I known the completeHandler function only execute after for loop looped six times.
is there anyway I could get the correct data to the correct array.
Thanks
Since you are using AIR to load data from the file-system, you don't have to do it asynchronously. You can load it synchronously like this:
function readTxtList(url:String):Array {
var file:File = File.documentsDirectory.resolvePath(url);
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var text:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
fileStream.close();
return text.split("\r\n");
}
Now you can just assign each value directly:
var oneArray:Array = readTxtList("1.txt");
var twoArray:Array = readTxtList("2.txt");
// etc
I recommend you to use Dictionary.
Create new Dictionary:
var dict:Dictionary = new Dictionary();
Bind an instance of the URLLoader class to the file name:
var urlRequest:URLRequest = ...
var urlLoader:URLLoader = new URLLoader();
dict[urlLoader] = listArray[i];
In the completeHandler you can get the file name:
trace(dict[e.currentTarget]);
Add if statements to reach your goal.
if (dict[e.currentTarget] == "1.txt")
oneArray = e.target.data.split(/\r\n/);
else if (dict[e.currentTarget] == "2.txt")
twoArray = e.target.data.split(/\r\n/);
...

Can i display multiple copies of a movieclip inside a array at once

Is there a way to make the code below work properly? When I use this code it only shows one movieclip:
var tempHead:head001 = new head001();
var mcArr:Array = new Array( tempHead );
var firstHead:MovieClip = mcArr[0];
firstHead.y = 30;
addChild(firstHead);
var secondHead:MovieClip = mcArr[0];
secondHead.y = 180;
addChild(secondHead);
`
You were just assigning a reference to the MovieClip. That'y, its not working.
First take instance of head001 class using new operator as much you want and store it to an array, then you can access very easily.
var tempHead: head001;
var mcArr: Array = new Array();
for (var i: uint = 0; i < 2; i++) {
tempHead = new head001();
addChild(tempHead);
mcArr.push(tempHead);
mcArr[i].y = mcArr[i].height * i;
}

AS3 Add array to another array

Example of the my problem.
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
// and the out come I want it to be is this
trace(array_1[0]) // 2,4,6,8,10,12,14,16,20,22,24
// I did try array_1[0] += array_2[0] but it didn't work currently
Any suggestion would be great.
This will perform what you are looking for and also allows you to add multiple rows of data to array_1 or array_2
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:Array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
var combinedArray:Array = new Array();
for( var i:int = 0; i < array_1.length; i++ ) {
combinedArray = combinedArray.concat(array_1[i]);
}
for( i = 0; i < array_2.length; i++ ) {
combinedArray = combinedArray.concat(array_2[i]);
}
trace(combinedArray);
As stated in the comments, you can use the concat method:
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
array_1[0] = array_1[0].concat(array_2[0]).concat(array_2[1]);
This, of course, is very messy looking. I am wondering why you are storing arrays inside of other arrays for no discernible reason.

Resources