Reference an element of an array using a variable in Actionscript 3 - arrays

I have a bunch of arrays (Q001, Q002, Q003...). Each array is a question plus 4 possible answers. I want to randomly choose which question to display. So I randomly assign an array name to the variable chosenQuestion. Then I try to write the first element of that array into a text box, like so:
txt.Question.text = [chosenQuestion][0];
For example, if the the chosen question is Q001 then it puts Q001 in the text box. But I really want in the text box is the first element of the array Q001.

Supposed that you have your 3 arrays of questions, so to get the question, you can do like this :
var question_01:Array = ['question 01', 'answer 01', 'answer 02', 'answer 03', 'answer 04'];
var question_02:Array = ['question 02', 'answer 01', 'answer 02', 'answer 03', 'answer 04'];
var question_03:Array = ['question 03', 'answer 01', 'answer 02', 'answer 03', 'answer 04'];
var selected_question:int = Math.ceil(Math.random() * 3); // gives : 1, 2 or 3
trace(this['question_0' + selected_question][0]); // gives : question 01, for example
You can also put your questions arrays into an array like this :
var questions:Array = [
['question 01', 'answer 011', 'answer 02', 'answer 03', 'answer 04'],
['question 02', 'answer 012', 'answer 02', 'answer 03', 'answer 04'],
['question 03', 'answer 013', 'answer 02', 'answer 03', 'answer 04']
];
selected_question = Math.floor(Math.random() * 3); // gives : 0, 1 or 2
trace(questions[selected_question][0]); // gives : question 02, for example
Hope that can help.

Related

how to add array of objects in react native

I have an array of object:
const totalItems = [
{
id: 0,
},
{
id: 1,
item: 'ITEM 1',
fat: 20,
prt: 30,
cho: 0,
cal: 300,
},
{
id: 2,
item: 'ITEM 2',
fat: 10,
prt: 15,
cho: 0,
cal: 150,
},
{
id: 3,
item: 'ITEM 3',
fat: 30,
prt: 10,
cho: 0,
cal: 310,
},
{
id: 4,
item: 'ITEM 4',
fat: 0,
prt: 0,
cho: 30,
cal: 120,
},
];
Now I want to add all the fat objects. I have used reduce() method. But it is not working. I am new at this. Kindly help me
you can map over the array and add the fat.
something like this
let totalFat = 0 ;
totalItems.map((item) => {
// I see there is no fat key value in first object id:0 so this check
// to make sure value exist
item.fat && (totalFat = totalFat + item.fat)
})
console.log(totalFat)
In SO while asking questions please give more details like the code you have tried, it's easier to understand that way.
You can use the method (forEach or map ).

ruby / sort an array of hashes from a defined index

Having the following array, it defines the sorting :
[300, 450, 345, 23]
And the following array, unsorted :
[
{id: 450, title: 'rand1'},
{id: 23, title: 'rand3'},
{id: 300, title: 'rand0'},
{id: 345, title: 'rand2'},
]
I'd like the first array to be a "rule" to sort my second array (probably by matching the id key).
How can i get achieve this cleanly ?
Naïve approach:
sorter = [300, 450, 345, 23]
input = [
{id: 450, title: 'rand1'},
{id: 23, title: 'rand3'},
{id: 300, title: 'rand0'},
{id: 345, title: 'rand2'},
]
input.sort do |h1, h2|
sorter.index(h1[:id]) <=> sorter.index(h2[:id])
end
#⇒ [
# {:id=>300, :title=>"rand0"},
# {:id=>450, :title=>"rand1"},
# {:id=>345, :title=>"rand2"},
# {:id=>23, :title=>"rand3"}]
or even simper:
input.sort_by { |h| sorter.index(h[:id]) }

Angular 2 declaring an array of objects

I have the following expression:
public mySentences:Array<string> = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
which is not working because my array is not of type string rather contains a list of objects. How I can delcare my array to contain a list of objects?
*without a new component which declaring the a class for sentence which seem a waste
I assume you're using typescript.
To be extra cautious you can define your type as an array of objects that need to match certain interface:
type MyArrayType = Array<{id: number, text: string}>;
const arr: MyArrayType = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
Or short syntax without defining a custom type:
const arr: Array<{id: number, text: string}> = [...];
public mySentences:Array<Object> = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
Or rather,
export interface type{
id:number;
text:string;
}
public mySentences:type[] = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
Another approach that is especially useful if you want to store data coming from an external API or a DB would be this:
Create a class that represent your data model
export class Data{
private id:number;
private text: string;
constructor(id,text) {
this.id = id;
this.text = text;
}
In your component class you create an empty array of type Data and populate this array whenever you get a response from API or whatever data source you are using
export class AppComponent {
private search_key: string;
private dataList: Data[] = [];
getWikiData() {
this.httpService.getDataFromAPI()
.subscribe(data => {
this.parseData(data);
});
}
parseData(jsonData: string) {
//considering you get your data in json arrays
for (let i = 0; i < jsonData[1].length; i++) {
const data = new WikiData(jsonData[1][i], jsonData[2][i]);
this.wikiData.push(data);
}
}
}
First, generate an Interface
Assuming you are using TypeScript & Angular CLI, you can generate one by using the following command
ng g interface car
After that set the data types of its properties
// car.interface.ts
export interface car {
id: number;
eco: boolean;
wheels: number;
name: string;
}
You can now import your interface in the class that you want.
import {car} from "app/interfaces/car.interface";
And update the collection/array of car objects by pushing items in the array.
this.car.push({
id: 12345,
eco: true,
wheels: 4,
name: 'Tesla Model S',
});
More on interfaces:
An interface is a TypeScript artifact, it is not part of ECMAScript. An interface is a way to define a contract on a function with respect to the arguments and their type. Along with functions, an interface can also be used with a Class as well to define custom types.
An interface is an abstract type, it does not contain any code as a class does. It only defines the 'signature' or shape of an API. During transpilation, an interface will not generate any code, it is only used by Typescript for type checking during development. - https://angular-2-training-book.rangle.io/handout/features/interfaces.html
public mySentences:Array<any> = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
OR
public mySentences:Array<object> = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
Datatype: array_name:datatype[]=[];
Example string: users:string[]=[];
For array of objects:
Objecttype: object_name:objecttype[]=[{}];
Example user: Users:user[]=[{}];
And if in some cases it's coming undefined in binding, make sure to initialize it on Oninit().
type NumberArray = Array<{id: number, text: string}>;
const arr: NumberArray = [
{id: 0, text: 'Number 0'},
{id: 1, text: 'Number 1'},
{id: 2, text: 'Number 2'},
{id: 3, text: 'Number 3 '},
{id: 4, text: 'Number 4 '},
{id: 5, text: 'Number 5 '},
];

AngularJS not selecting integer values

Does anyone know why AngularJS not selecting options on <select multiple> if ng-model is an array of integer?
$scope.selected = [1, 3]
$scope.options = [
{id: 1, title: 'option 1'},
{id: 2, title: 'option 2'},
{id: 3, title: 'option 3'}
]
Everything works just fine if $scope.selected = ['1', '3'] is array of string. I need to get it working without using ng-options.
Here is JSFiddle:
http://jsfiddle.net/maxflex/3jfk8/19/

Angular gantt customize data fields

Does angular gantt plugin allow to change the column header labels? I tried to change the side table headers but couldn't. If it doesn't allow what other gantt chart can I use? At the side table, I need to show work station, id and type. In the gannt chart, I need to show the particular ids under the respective priority numbers. Any suggestions, please?
You can change or defined the column header labels at property Options.
$scope.options = {
...
treeTableColumns: ['from', 'to', 'myColumn'],
columnsHeaders: { 'model.name': 'Name', 'from': 'From', 'to': 'To', 'myColumn': 'My column name' },
columnsClasses: { //css
'myColumn': 'myclass'
},
//defined content here. ex: String, checkbox, radio, ...
//ex: <input type="checkbox" id={{row.model.id}} ng-model="row.model.myColumn">
columnsContents: {
'myColumn': '{{row.model.myColumn}}',
...
},
};
$scope.myData = [{
id: 1,
name: 'Finalize concept',
myColumn: 'content',
tasks: [{
id: 'Finalize concept',
name: 'Finalize concept',
priority: 10,
color: '#F1C232',
from: new Date(2013, 9, 17, 8, 0, 0),
to: new Date(2013, 9, 18, 18, 0, 0),
progress: 100
}]
}, {
id: 2,
name: 'Development',
myColumn: 'content',
children: ['Sprint 1', 'Sprint 2', 'Sprint 3', 'Sprint 4'],
content: '<i class="fa fa-file-code-o" ng-click="scope.handleRowIconClick(row.model)"></i> {{row.model.name}}'
} ]
https://www.angular-gantt.com/configuration/attributes/
Here yo have the answer about all you can do.
You cant change the column header labels but you can select between all the formats that you have:
$scope.headersFormats = {
'year': 'YYYY',
'quarter': '[Q]Q YYYY',
month: 'MMMM YYYY',
week: 'w',
day: 'D',
hour: 'H',
minute:'HH:mm'
};

Resources