How to compare values between a 2 dimensional and one dimesional array? - multidimensional-array

Array1:
[[P01, 153425], [P02, 3951990], [P03, 106658], [P04, 4563594], [P05, 60198], [P07, 326292], [P08, 1265], [P09, 108293], [P10, 183698], [P11, 5084]] this comes from BigQuery.
Array2:
[[P01], [P02], [P03], [P04], [P05], [P06], [P07], [P08], [P09], [P10], [P11]]
this array is fetched from google sheet column using sheet.getSheetValues(3,1,11,1);
Based on this i should update a column in google sheet based on the matching values between Array1 and Array2:
eg: [153425,3951990,106658,4563594,60198,326292,1265,108293,183698,5084]
if value does not match the corresponding value should remain empty.

Compare Array1[i][0] with Array2[j] inside for loop, if it is equal take the value of Array1[i][1]
for(var i=0; i<Array1.length; i++){
for(var j=0; j<Array2.length; j++){
if(Array1[i][0] == Array2[j]){
//do something
//if it can be equal to only one number, you can break it here
}
}
}
EDIT
Then you should compare Array2[i] with Array1[j][0]
for(var i=0; i<Array2.length; i++){
var isExist = false;
for(var j=0; j<Array1.length; j++){
if(Array2[i] == Array1[j][0]){
//do something
//if it can be equal to only one number, you can break it here
isExist = true;
}
}
if(!isExist){
//do something if it does not exist
}
}

Related

How to display new List item?

next 2 Mp3's on the list, can someone help me with this?
I want to display all the Mp3's that are left
You could use Console.ReadKey to wait for any input key, and use the same logic to print rest of the records.
int maxCount = 3;
for (int i = 0; i < maxCount && i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
Console.ReadKey(); // wait for key-in
// print remaining records.
for (int i = maxCount ; i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
You could use the Console.ReadKey and loop through the list again and start from the index where you left off initially.
for (int i = maxCount; i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
You could also make it a seperate method so you'll only have one place in your code to loop through your list and you can easily modify how many items you want to print and from what index to start printing.
public void ShowItemsInConsole(int firstIndex, int lastIndex)
{
for (int i = firstIndex; i < lastIndex && i < Mp3spelers.Count(); i++)
{
Console.WriteLine(Mp3spelers[i].ID);
Console.WriteLine(Mp3spelers[i].make);
Console.WriteLine(Mp3spelers[i].mbsize);
Console.WriteLine(Mp3spelers[i].model);
Console.WriteLine(Mp3spelers[i].price);
Console.WriteLine(Mp3spelers[i].voorraad);
}
}

Google maps API checking is element exists

I'm calling google maps geocode and looping over the address_components struct and tring to output the city name. I'd like to store the postal_town as the city name but if it does not exist then use the administrative_area_level_2 instead.
The problem is that even if a postal_town is returned the administrative_area_level_2 is still being used. Can anybody see what i'm doing wrong?
for(var i=0, len=result.address_components.length; i<len; i++) {
var ac = result.address_components[i];
if(ac.types.indexOf("postal_town") >= 0){
state = $('#sharesparestepAddressCity').val(ac.long_name);
}else if(ac.types.indexOf("administrative_area_level_2") >= 0){
state = $('#sharesparestepAddressCity').val(ac.long_name);
}
}
If both exist, your code will return the last one found. Add a break to stop processing when you find the one you want.
for(var i=0, len=result.address_components.length; i<len; i++) {
var ac = result.address_components[i];
if(ac.types.indexOf("postal_town") >= 0){
state = $('#sharesparestepAddressCity').val(ac.long_name);
break;
} else if(ac.types.indexOf("administrative_area_level_2") >= 0){
state = $('#sharesparestepAddressCity').val(ac.long_name);
}
}

How to create an object from 2 arrays?

So I hava array Links and array Params with same langth N
So what I need is to create an object where for each link from Links I will be able to see
param from Params
And than for example to be abble to call something like
for each( item in object)
if (item.param == "some value") {
// some code
} else...
How to do such thing (Code exaMple, please)
From Array: You could first build a list with elements composed of a item and a param (supposing the length is indeed the same for both lists)
var items:Array = new Array();
for(var i:uint = 0; i < links.length; i++) {
links:Array .push({link:links[i], param:params[i]});
}
You can then filter them easily:
items.forEach(checkValue);
for(var i:uint = 0; i < items.length; i++) {
if (items[i].param == "some value") {
// some code
} else{
...
}
}

save state of a dataGrid: visible columns, columns width and order

I want to save remotely (on a database) the state (visible columns, columns width and order) of a Flex3 DataGrid.
For width and visibility I can simply save them by accessing each column attribute.. ugly but possible..
But for the order? Do I have to create the dataGrid dynamically??
Any idea is appreciated
thanks
In my case I have saved the order by header name (I'm making an assumption that your DataGrid always has the same columns and header names).
for (var n:Number = 0; n< datagrid.columns.length; n++)
{
var thiscol:DataGridColumn = DataGridColumn(datagrid.columns[n]);
colArray.addItem(thiscol.headerText);
}
Then I can restore the column order by retrieving the ordered list of column headers, and swapping the position of columns in the datagrid as required.
for (var n:Number = 0; n < colArray.length; n++)
{
moveColumnTo(String(colArray.getItemAt(n)), n);
}
I have defined a function moveColumnTo() to perform the switch.
private function moveColumnTo(columnName:String, columnIndex:Number):void
{
// Find current column position
var i:Number = -1;
for (var n:Number = 0; n < datagrid.columns.length; n++)
{
if (DataGridColumn(datagrid.columns[n]).headerText == columnName)
{
i = n;
break;
}
}
if (i == -1 || i == columnIndex) return; // Don't shift column
this.mx_internal::shiftColumns(i, columnIndex); // Shift column to required position
}
Why can't you just save the order as you're looping through each column?
for(var i:int = 0; i < dataGrid.columns.size; i++)
{
var column:DataGridColumn = dataGrid.columns[i];
arr.push({column.visible, column.width, i});
}

JsonArray in silverlight to javascript

I'm creating a JsonArray such as:
JsonArray jsonValues = new JsonArray();
for( int i = 0; i < values.Count; i++ )
{
var someSingleValue = values[i];
jsonValues.Add( string.Format( "Name: {0}", someSingleValue ) );
}
After that I'm shipping json values to my javascript in .aspx page via call:
HtmlPage.Window.Invoke("call", jsonValues);
The call works and it gets there, however I have no idea how to iterate over those values, i.e extract them.
I've tried: (in javascript)
for (var x in somevalues){ alert(somevalues); }
I also tried:
for(var i = 0; i < somevalues.length; i++) {
alert(somevalues[i]);
}
but it crashes.(in both cases)
any ideas?
Are you using the eval method to serialize the string to a JSON object?
function call(somevalues){
//somevalues is currently just a string.
var results = eval("(" + somevalues +")");
//results now should contain your array as a JSON object.
//and you should be able to iterate over it at this point.
for(var i = 0; i < results.length; i++){
alert(results[i]);
}
}
Assuming somevalues is truly an array, you do it like this:
for(var i = 0; i < somevalues.length; i++) {
// do something with somevalues[i]
}
What you did was tell JavaScript to iterate over the properties of the somevalues object, which is similar, but not the same, as the iteration using a regular for loop.
EDIT: I am willing to bet your variable, somevalues is a string. Just do alert(somevalues) and see what happens.

Resources