when I am initializing array globally (outside the method) its working fine. but when I am initializing the same array inside the method its throwing error unexpected token. you can see into code for location of array. this is array calculateResult[] = [];
Screenshot of the error
private log: string ='result';
private kw: string = 'kw';
private frame: number = 0;
public finalResult[] = [];
//here this array is working fine
calculateResult[] = [];
DisplayResult(){
//if i initialize this array here, it's throwing error
// calculateResult[] = [];
if(some_conditions_true){
alert();
this.log = '1SE0 070-2NC70' '\n';
this.kw = '.37' '\n';
this.frame = '71' '\n';
this.calculateResult[0] = this.log;
this.calculateResult[1] = this.kw;
this.calculateResult[2] = this.frame;
this.finalResult.push(this.calculateResult);
for(i=0;i < this.finalResult.length;i++){
console.log(this.finalResult[0][0]);
console.log(this.finalResult[0][1]);
}
}
The first declaration is considered as a class property, so it's correct.
The second one, is incorrect because it is inside the class method and thus should be either declared as
let calculateResult=[];
if you intend to declare a new array of that name,
either addressed as the class property declared above as
this.calculateResult = ...
So keep the first one: calculateResult = [];
If you keep it commented, this.calculateResult[0] will be undefined in the class method, you cannot refer to it.
You should initialize as
DisplayResult(){
this.calculateResult = [];
}
Related
I am trying to create dynamically generated variables based on another variable, such as:
var limit = 2;
$scope.blank_item = [];
$scope.create_vars = function(limit){
for(i=0; i<limit; i++){
eval('var item' + i) = $scope.blank_item;
};
};
And this should give me 3 new variables like below:
var item0 = [];
var item1 = [];
var item2 = [];
However, when I try to do this, I get the following error message:
ReferenceError: Invalid left-hand side in assignment
Is there something fundamentally wrong with my logic?
This has nothing to do with Angular. The value passed to eval() has to be one or more statements, not part of a statement. And you certainly can't pass part of a statement to eval() and then assign a value to it.
Even you could do that, it would just be creating temporary variables that disappeared as soon as your function finished executing.
Ditch the eval() and use an array:
var limit = 2;
var items = [];
$scope.blank_item = [];
$scope.create_vars = function(limit){
for(i=0; i<limit; i++){
items[i] = [];
};
};
Im trying to access an individual tile inside of my 2d array of tiles
Ive created my array like so:
private var map:Array = [[25,25]];
ive tried several ways including:
map[1][1] = 1;
all that does is give me this:
ReferenceError: Error #1056: Cannot create property 1 on Number.
ive also tried:
map[[1,1]] = 1;
nothing happends that I can tell
The only way that ive tried so far that gets me a result thats not an error is:
map[1,1] = 1;
The issue here is this selects the whole row.
Any help would be apreciated..thanks!
This is not correct way to create 2D array:
private var map:Array = [[25,25]];
This array contains one array which contains two elements. You can't address to the second element like this:
map[1][1] = 1;
because the second element (array) of map doesn't exist.
You can create 2D array this way:
var map:Array = [];
for (var i:int = 0; i < rows; i++)
{
map[i] = [];// this line adds new row to the 2D array
// To fill the array by zeros add next loop
for (var j:int = 0; j < cols; j++)
{
map[i][j] = 0;
}
}
To start, I think that to get the error mentioned in your question, your array should looks like this :
var map:Array = [
[25, 25], // map[0] = [25, 25] ( array of two elements )
35 // map[1] = 35 ( just a simple number )
];
So, if you write :
trace(typeof map[1]); // gives : number
You will get : number, and that's why you can not right : map[1][1] = value; and it's normal that you got the #1056 error.
Here, I don't know if you meant assign the value 1 to your 2nd 25 of map[0] or you want really add or edit map[1][1], in the 1st case, you can simply write :
map[0][1] = 1; // gives : map[0] = [25, 1]
In the 2nd case, you can do :
map[1] = [any_other_value, 1]; // gives : map[1] = [any_other_value, 1]
Last remark, forget that you got an error and suppose that your map array was just:
var map:Array = [
[25, 25]
];
Here, you can not also write map[1][1] = value;, why ? Let's use the same method :
trace(map[1]); // gives : undefined
So sure you can not add a property to an undefined, that's why when you write :
map[1][1] = value;
You will get an #1010 error : "undefined has no properties. ".
Of course here, we should firstly create map[1] :
map[1] = []; // or directly map[1] = [any_other_value, value]
And then :
map[1][1] = value;
Hope that can help.
I am trying to create a matching game where one object in the array hitBoxes is matched to one object in the array hitBoxes2. I have tried to convert the instance name into a string and then used the substring method to match the LAST number in the instance name, if its a match they win. Right now I'm getting the error
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at MethodInfo-499()
I'm wondering if anyone can help me. Thanks!
var left:String;
var correct:MovieClip = new Correct;
var isClicked:Boolean = false;
var leftClicked:int = 0;
p3.nextPage.buttonMode = true;
p3.nextPage.addEventListener(MouseEvent.CLICK, nextPage);
function nextPage(MouseEvent):void{
removeChild(p3);
}
var hitBoxes:Array = [p3.a1, p3.a2, p3.a3, p3.a4, p3.a5, p3.a6, p3.a7, p3.a8];
var hitBoxes2:Array = [p3.b1, p3.b2, p3.b3, p3.b4, p3.b5, p3.b6, p3.b7, p3.b8];
for (var h:int = 0; h < hitBoxes.length; h++){
hitBoxes[h].buttonMode = true;
hitBoxes[h].addEventListener(MouseEvent.CLICK, matchingLeft);
}
for (var h2:int = 0; h2 < hitBoxes2.length; h2++){
hitBoxes2[h2].buttonMode = true;
hitBoxes2[h2].addEventListener(MouseEvent.CLICK, matchingRight);
}
function matchingLeft(e:MouseEvent):void{
var left = String(e.currentTarget.name);
isClicked = true;
trace(left);
}
function matchingRight(e:MouseEvent):void{
var right:String = String(e.currentTarget.name);
trace(right);
if(isClicked == true && left.substring(3,3) == right.substring(3,3)){
trace("matched");
}
}
According to your code variable "left" is null at matchingRight method, because matchingLeft uses its local variable with name "left", and top-level "left" still has its default value.
also String.substring method is used incorrectly:
var name:String="p3.a1";
trace(name.substring(3, 3)); // this will always output empty string ""
trace(name.substring(4, 5)); // this will output "1" string
in conclusion I'd advise to use array indices (integers) instead of strings when calculating "matched" condition, substring operation and string comparison are CPU intensive.
so I have writing a function that returns objects on the stage and puts them into an array. and the function works fine until i call the function on more than one object name, meaning if im in the root class, and I call this function on object1 lets say it will add all the object one's from the stage, but if i call it on object2 it will throw an error, which makes some sense, i guess it means that it is not adding it to a unique array, but im not sure how to do that.
would it be a good idea to maybe make a multidimensional array? if that is the case would it be too slow?
here is the function code:
public function findObjects(objectName, objLocation, bVisible = false):Array{
for (var i = 0; i < objLocation.numChildren; i++){
var nObj=objLocation.getChildAt(i);
if (nObj is objectName){
// add to array and make invisible
obj.push(nObj);
nObj.visible=bVisible;
}
}
return obj;
}
any help with this would be greatly appreciated.
Try this:
public function findObjects(type:Class, target:DisplayObjectContainer, bVisible:Boolean=false):Array
{
var out:Array = [];
for(var i:int = 0; i<target.numChildren; i++)
{
var obj:DisplayObject = target.getChildAt(i);
if(obj is type)
{
out.push(obj);
obj.visible = bVisible;
}
}
return out;
}
And then based on your code, the implementation would probably be:
obj = findObjects(MovieClip, container);
I have created an array as shown below
protected function getMyArray(dataArray:Array):Array
{
var labelList:Array=new Array;
for each (var property:Object in dataArray)
{
if (labelList[property.bucketTime] != property.bucketTime)
labelList[property.bucketTime]=property.bucketTime;
}
return labelList;
}
Is it possible to sort the array labelList based on property.bucketTime?
Edit: Sample input dataArray will be like this :
var tempObj:Object = new Object;
tempObj.bucketTime = DateField.stringToDate("30-01-2010", "DD-MM-YYYY").time;
tempObj.score = 76;
dataArray.addItem(tempObj);
tempObj = new Object;
tempObj.bucketTime = DateField.stringToDate("13-02-2010", "DD-MM-YYYY").time;
tempObj.score = 21;
dataArray.addItem(tempObj);
tempObj = new Object;
tempObj.bucketTime = DateField.stringToDate("30-03-2010", "DD-MM-YYYY").time;
tempObj.score = 10;
tempArry.addItem(tempObj);
Unless bucketTime is a number; then you aren't actually populating the array. You're just adding properties to the Array Object, almost like it were a Dictionary. I've seen a Dictionary called Associative Array's and Structures in other languages.
If that is the case, an you're using the Array class as a dictionary, then there is no way to sort it. The very nature of such a structure is that they are not sortable.
However, if property.bucketTime is a number, and you are trying adding items to the array as if they were an array, you can sort using the Array.sort or Array.sortOn methods.
Atlast I sorted the labelList Array.Please find my solution below. Let me know if there is any better way to achieve this.
var termObject:Object = ObjectUtil.getClassInfo(labelList);
var termsArray:Array = termObject.properties;
var sortedColumnArray:Array = new Array;
for each(var term:Object in termsArray){
if(labelList.hasOwnProperty(term)){
sortedColumnArray.push(labelList[term]);
}
}