Retrieve Coordinates from Array - maps

I would like to put all my coordinates into an array and have a loop display each one but with a Google function.
Here's the code, which draws the points on google map:
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
I would like to be able to do something like this:
var arrPos = new Array([37.772323, -122.214897], [21.291982, -157.821856], [-18.142599, 178.431], etc. );
var flightPlanCoordinates = [ +
for (i=0; i<arrPos.length; i++){
new google.maps.LatLng(arrPos[0]) + ", "
}
+ "];"
I know you can put a loop in an array but is there an alternate method to retrieving the points from the array??
tks

Like this:
For(var i in arrPos){
new google.maps.LatLng(arrPos[i][0],arrPos[i][1]);
}
Greets
Thomas van Latum

Related

Is there a way to filter an array for strings in google apps script?

I am trying to filter the array 'employee_name' consisting of NaNs and one string element, to exclude any element BUT the string. The context is that I have a spreadsheet containing employee's birth dates, and I'm sending an email notification in case there's a birthday two days from today. My variables look like this:
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Employees');
var range = ss.getRange(2, 1, ss.getLastRow()-1, 1); // column containing the birth dates
var birthdates = range.getValues(); // get the `values` of birth date column
var today = new Date ();
var today = new Date(today.getTime());
var secondDate = new Date(today.getTime() + 48 * 60 * 60 * 1000);
var employee_name = new Array(birthdates.length-1);
And the loop:
for (var i=0;i<=birthdates.length-1;i=i+1){
var fDate = new Date(birthdates[i][0]);
if (fDate.getDate() == secondDate.getDate() &&
fDate.getMonth() == secondDate.getMonth()){
//define variables for outgoing email
for (var j=0; j<=birthdates.length-1;j=j+1){
employee_name[j] = [NaN];
}
employee_name[i] = ss.getRange(i+2,6);
employee_name[i] = employee_name[i].getValues();
}
}
after which the array in question looks like this
Logger.log(employee_name);
[[[Mia-Angelica]], [NaN], [NaN], [NaN], ..., [NaN]]
I have already tried the filter(Boolean), but this isn't working:
employee_name_filtered = employee_name.filter(Boolean);
Logger.log(employee_name_filtered);
returns [[[Mia-Angelica]], [NaN], [NaN], [NaN], ..., [NaN]].
I have also tried filling the non-string array entries with numeric values (instead of NaN) and then apply
employee_name_filtered = employee_name.filter(isFinite);
Logger.log(employee_name_filtered);
returns [[1.0], [2.0], [3.0], ..., [72.0]], so this filter method is working, but then I would need the 'inverse' of that because I want to keep the string.
I need the array within array to store the values at the position of the counter variable where the condition's met (similar to How to store data in Array using For loop in Google apps script - pass array by value).
This is my first time posting a question on SO, so if I overlooked any 'rules' about posting, just let me know and I will provide additional info.
Any help will be appreciated!
EDIT:
what I would like to receive in the end is simply
[[Mia-Angelica]].
The array you are using a 2 dimensional array - meaning it's an array of arrays so the filter method you are using cannot be applied in the same manner.
For this, I suggest you try the below snippet.
function cleanArray() {
var initialArray = [
['Mia-Angelica'],
['Space'],
['2'],
[NaN],
[NaN],
[NaN],
[NaN]
];
var finalArray = [];
for (let i = 0; i < initialArray.length; i++) {
var midArray = initialArray[i].filter(item => (Number.isFinite(item) && item.id !== 0) || !Object.is(item, NaN));
finalArray.push(midArray);
}
console.log(finalArray.filter(item => item != ''));
}
Note
Please bear in mind that getValues will return an Object[][] which is a two-dimensional array of values.
Reference
Apps Script Range Class;
Array.prototype.filter().

How to acces array object in nodejs?

I am getting this req in my req.body . I want to parse the details .Below is my req.
"[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
I want output like
number:
cmdb_ci:
category:
How do i parse this array object in nodejs. Please help
Use JSON.parse() like this:
var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
console.log('number : ' + aObjList[i].number);
console.log('cmdb_ci : ' + aObjList[i].cmdb_ci);
console.log('category : ' + aObjList[i].category);
}
You Can Use
JSON.parse(req.body);
This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.
You can use
var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;

Linking two arrays in Actionscript 3

Very new to actionscript,
Im trying to link two arrays. Basically I have a word array of 8 words and I have a movie clip array of 8 movieclips. My aim is to link the two arrays so that the user must click the right movie clip that matches the word that was displayed on screen.
All help is greatly appreciated!!
var listAry:Array = [];
var orangeJuice:Object = new Object();
orangeJuice.name= "Orange Juice";
orangeJuice.matchingImage=oj;
listAry[0]=orangeJuice;
////etc etc
There you go buddy. Hope that helps if you have any questions just ask.
Another way to do this is with a Dictionary.
var foodionary:Dictionary = new Dictionary();
foodionary["Orange Juice"] = oj;
foodionary["Sandwich"] = sand;
//etc...
for(var key:String in foodionary) {
trace(key + " matches with " + foodionary[key].id); //assuming your images have ids
}
For random access, though, you'll still need an array (or a Vector):
function displayword(){
randomnumber = Math.floor(Math.random() * randomlistword.length);
trace("random number = " + randomnumber);
var chosenword = randomlistword[randomnumber];
randomword.text = chosenword
randomword.img = foodionary[chosenword];
randomlistword.splice(randomnumber, 1);
trace("randomlistword array: " + randomlistword);
}//close displayword function

Push instance name of movieClip into array using a for-loop

I have a bunch of movieclips on the stage with instance names ball1 - ball200. I was hoping I didn't have to create an array and manually set all the instance names into the array
ballArray = [ball1, ball2,ball3, etc];
I was trying to get a for loop to cycle through and add each instance name to my array like so:
function createTheArray():void{
for(var i:int = 1; i < 20;i++){
ballArray.push(ball + i);
trace(newArray[i])
}
}
But I keep getting back undefined array index's. It also tells me that I doesn't know what "ball" is. How would you use part of a instance name and combine it with the index value of the loop. So that the first time through you get ball1 as the first index value of your array?
Dragging out 200 balls onto the timeline and giving them instance names doesn't sound like much fun!
BEST OPTION:
right click the ball object and go to the properties, click "export for actionscript" and give it a unique name. (Lets call it MyBall for this example)
in your timeline code do this:
var ballArray:Vector.<MyBall> = new Vector.<MyBall>();
for(var i:int=0;i<200;i++){
ballArray.push(new MyBall());
addChild(ballArray(ballArray.length-1));
}
NEXT BEST OPTION
if all your balls are on the timeline already, you can still do the step from above (export for actionScript and give it a name) but do the following code:
var ballArray:Vector.<MyBall> = new Vector.<MyBall>();
var i:int = numChildren;
while(i--){
if(this.getChildAt(i) is MyBall) ballArray.push(this.getChildAt(i) as MyBall);
}
ANOTHER OPTION
If your balls are not all the same library objects, if you put them all as the only objects in a movie clip container (let's say you gave it the instance name ballContainer, you can still use this code so you don't have to give them instance names:
var ballArray:Vector.<DisplayObject> = new Vector.<DisplayObject>();
var i:int = ballContainer.numChildren;
while(i--){
ballArray.push(ballContainer.getChildAt(i));
}
You can use a string in brackets to get a property of an object. In your case, your object is referred to as this. So your syntax for getting a ball is this["ball"+index].
Try this:
function createTheArray():void{
for(var i:int = 1; i < 20; i++){
ballArray.push(this["ball" + i]);
}
trace(ballArray);
}
Referencing Properties by String isn't really a great practice though. If it's possible to create your balls dynamically as well, that would be a better implementation. You can create a ball MovieClip on your timeline, and select Export For ActionScript in the properties. Then you can use this code to instantiate 20 or more balls:
//add 20 balls to stage
var ballArray:Array = [];
for(var i:int = 0; i < 20; i++){
var ball:Ball = new Ball();
addChild(ball);
ballArray.push(ball);
}
trace(ballArray);

AS3: Sort array descending while keeping strings in correct order

I'm not sure whether there is a simply answer to this but I am assuming there isn't, hence I'm here.
Basically, I want to run a very simple high score table which keeps track of the high scores of a game, but also displays the correct names beside each scores.
This is all easy but I want to be able to do this with just the one array.
For example, I have this code:
var d:Array;
var e:Array;
d = "827-Harry".split("-");
d.push("918-John".split("-"));
trace(d)
Which correctly results this trace:
827,Harry,918,John
My question is, how can I use Array.sort() (or similar) in such a way that the following is produced:
d = 918, John, 827, Harry
It can't be specific to this example. That is, it needs to work with custom names and dynamic scores.
Cheers in advance!
Harry.
create an associative array and use sortOn():
var highscores:Array = new Array();
highscores.push({score: 827, player: "John"});
highscores.push({score: 918, player: "Harry"});
highscores.sortOn("score", Array.DESCENDING | Array.NUMERIC);
for (var i:int = 0; i < highscores.length; i++)
{
trace(highscores[i].score, highscores[i].player);
}
Wouldn't recommend you store your high scores like that but I don't know your reasons so here's exactly how to do what you want.
public function sortArray(arrUnsorted:Array):Array
{
var arrLocal:Array = new Array();
var arrSorted:Array = new Array();
for (var i:int = 0; i < arrUnsorted.length; i += 2)
{
arrLocal.push( { score:int(arrUnsorted[i]), name:arrUnsorted[i + 1] } );
}
arrLocal.sortOn("score", Array.DESCENDING | Array.NUMERIC)
for each(var obj:Object in arrLocal)
{
arrSorted.push(String(obj.score), obj.name);
}
return arrSorted;
}
Then it's as simple as:
var arrUnsorted:Array = ["827", "Harry", "918", "John"];
var arrSorted:Array = sortArray(arrUnsorted);
trace(arrSorted); // 918,John,827,Harry
Hope this is what you're after.

Resources