Cleaning up angular.forEach / if statements into more concise code? - arrays

I got the result I wanted - but I am doing it in a way that is just, wow.
I keep finding the right array within an array, and then doing a foreach and if to find the next correct array. Is there a more sanitary way to do this? My code is below:
angular.forEach($scope.bitrates, function(bitrateset) {
if(bitrateset.resolution == cam.camera.resolution){
angular.forEach(bitrateset.stats, function(stat) {
if(stat.activity == cam.activity){
angular.forEach(stat.fps, function(fps) {
angular.forEach(fps.range, function(range) {
if(range == cam.fps){
cam.bitrate = fps.bitrate;
}
else{
}
});
});
}
});
}
});

Related

How to check whether array includes certain value google Apps Script

I'm using range.getValues() to load an array in Google Apps Script.
I want to loop through the array and do something if the array includes the value I'm looking for.
if (array.include("abc")){*doSomething*}
The problem is when I use this method, the array contain another arrays, so array.include() doesn't work.
I can use a workaround like this:
for (var i = 0; i<=array.length; i++){
if (array[i] == "abc"){*doSomething*}
I wonder are there a better way to do it?
I tried to use array.indexOf() but it returns -1 for 0th value, which is weird
function find() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const haystack=sh.getDataRange().getValues();
const needle="44";
let found = false;
haystack.forEach(r=>{
r.forEach(c=>{
if(c==needle) {
found=true;
}
})
});
if(found) {
SpreadsheetApp.getUi().alert('Needle was found in haystack');
}
}
or
function find() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const haystack=sh.getDataRange().getValues();
const needle="44";
let found = false;
haystack.forEach(r=>{
if(~r.indexOf(needle)) {
found=true;
}
});
if(found) {
SpreadsheetApp.getUi().alert('Needle was found in haystack');
}
}
Recommendation:
Alternatively, you can also try this way:
function arrayCheck(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var arr = ss.getDataRange().getValues(); //Get all values from sheet
Logger.log(arr);
arr.forEach(function(res) {
if(res == 'abc'){
Logger.log("Found a match");
//do something
}else{
Logger.log(res+" does not match abc");
}
});
}
Here's a sample sheet where I've got all the array values from:
Here's the result:

How to check if Value of an array is true in a cell(Google Script)

I am having issues checking if the string value in my array is true in a cell.
function myFunction() {
var People = ['Amanda', 'John'];
for (var n in People )
{
if( People[0] == true);
Logger.log("BOOKED");
}
else{
Logger.log("FREE");
}
}
Plenty of issues with the code!
Does this do it ?
function myFunction() {
var People = ['Amanda', 'John'];
for (var n=0;n<People.length;n++) {
if( People[n] ) {
Logger.log("BOOKED");
}
else {
Logger.log("FREE");
}
}
}
I'm not sure if you just want to test for members of the array being present, or if you want to test actual names in the array

Return promise inside the for loop

I have a logic like below,
getSpecificCell: function(tableObject, rowText, columnCss) {
var ele = element.all(by.repeater(tableObject)).count().then(function(count) {
for (var i = 0; i < count; i++) {
return element(by.repeater(tableObject).row(i)).getText().then(function(txt) {
if (txt.indexOf(rowText) !== -1) {
return element(by.repeater(tableObject).row(i)).element(by.css('[' + columnCss + ']'));
}
});
}
});
return ele;
}
But it is returning the value in first iteration itself.
Is that possible to return the promise inside this kind of for loop or do we have any other solution for this?
First, you don't need to use for loops with an ElementArrayFinder. That's what the each() method is for.
Second, you shouldn't need to loop at all. It sounds like you should be using filter() to get the table cells that match your specification, though I'm not sure what exactly you're trying to accomplish.
var table = element.all(by.repeater(tableObject));
// list is an ElementArrayFinder of all elements that matched the filter
var list = table.filter(function (elem) {
return elem.getText().then(function (text) {
return txt.indexOf(rowText) !== -1
})
});
// do something with list
list.count().then(function (count) {
console.log(count);
});

$scope.$watch callback executes after a delay

I have the following:
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days=[];
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
//$scope.$apply();
/*if(typeof(this.duration.days)!='object')
{
this.duration.days=[];
}*/
var index = this.duration.days.indexOf(day);
if (index == -1) {
//alert('test2');
this.duration.days.push(day);
}
else {
this.duration.days.splice(index, 1);
}
}
In this, when I do $scope.duration.dayPreference = 'selected'; I expect the line below it to have the this.duration.days set to a blank array. But it doesn't. Upon a closer inspection, I found that the callback in the $watch runs after the line below the assignment.
It may be very probable that, $watch may be using some kinda timers internally. What should be the way to do it then.
The watch won't be triggered until the digest is run. This will be after your entire function is compete.
If you consider that AngularJS is itself written in JavaScript, there would be no way for it to react to your setting of a property at the time. You are using the thread yourself. It can only wait for you to finish and then react.
As for what to do instead...
Perhaps you could call that watch function manually?
Or maybe the code which expects the empty array should belong inside the watch?
Watch will trigger on the $digest, which will occur after current cycle/code finishes running. You need to figure out a way of rearranging your code that handles things asynchronously. One possible quick solution might be:
var selectedDays = [];
$scope.$watch('duration.dayPreference',function(value){
console.log(value);
if(value=='every')
{
that.duration.days = 1;
}
else if(value=='selected')
{
//alert('test');
that.duration.days = selectedDays;
}
else if(value=='everyday')
{
that.duration.days='everyday';
}
});
this.selectDay = function (day) {
$scope.duration.dayPreference = 'selected';
var index = selectedDays.indexOf(day);
if (index == -1) {
//alert('test2');
selectedDays.push(day);
}
else {
selectedDays.splice(index, 1);
}
}

Unity unityscript Error

I have an array learnnum that looks like [0,1,1,0,1,1,1,1,0].
I need to basically ask the user for an input Left Mouse Button or Right Mouse Button. If Left, then the values of learnnum of [i] is flipped, else nothing happens. I only do this for i=1,3,5,7. I have written the below code, but it does not work properly, instead of going for all the 4 conditions... it directly goes to 4. It seems that it is not waiting for the input conditions... Is there any way I can correct this?
function changeNumba(i)
{ //check1=true;
print ("PRINTT "+check1);
while(!Input.GetButtonDown("Fire1") && !Input.GetButtonDown("Fire2"))
{
if(Input.GetButtonDown("Fire1"))
{
check1++;
}
if(Input.GetButtonDown("Fire2"))
{
learnednum[i]=0 ? 1 : 0;
check1++;
}
}
}
function changelearn()
{
//FIRST STEP
//if(check1)
if(move1==9 && check1==0)
{changeNumba(1);
}
//SECOND STEP
if(move1==9 && check1==1)
{changeNumba(3);
}
if(move1==9 && check1==2)
{changeNumba(5);
}
if(move1==9 && check1==3)
{changeNumba(7);
}
}
var check1=0;
//1,3,5,7
function Update () {
if(move1==9)//this is just a game condition. Do not bother about it.
{
changelearn();
}
}
from looking at the unity script api:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html
you should not have a while() loop inside of your Update() method.
change changeNumba() as follows:
function changeNumba(i)
{
if(Input.GetButtonDown("Fire1")){
check1++;
}
if(Input.GetButtonDown("Fire2")){
learnednum[i] = learnednum[i]==0 ? 1 : 0;
check1++;
}
}

Resources