I want to do something like this. Do you have ideas for this?
=== a ===
if( Something ){
=== b ===
{
else
{
go back to === a ===
}
Thanks.
It is usual while loop.
$something = false;
while (!$something) {
//here are ====a==== actions
}
//here are b actions
You want something like this:
while (true) {
if( Something ){
=== b ===
}
else
{
=== a ===
}
if (we're done) {
break;
}
}
Alternatively, you could use a proper condition in your while loop to exit.
Related
I'm having trouble updating this code from angularjs ad angular 2, thank you for any help
if (settings != null && settings.checkSubscriptionForVip === true) {
return this.user.hasVipAccess().then(function(hasVipAccess) {
hasVipAccess ? deferred.resolve() : deferred.reject("NO_VIP");
return;
});
} else {
deferred.resolve();
return;
}
You neeed to have a boolean variable defined in your component and assign the result in to that,
hasVipAccess : boolean = false;
if (settings != null && settings.checkSubscriptionForVip === true) {
this.user.hasVipAccess().then(function(access) {
this.hasVipAccess = access;
});
} else {
this.hasVipAccess = access;
}
However if you do not need to set boolean variable, just return the result
I need to sort a table for an Angular project. The catch is that for some of the values in the table, I need to sort by a direct property of the objects in the array, but for others I need to sort by a child of that direct property.
For example, I have associate.lastname for one column and associate.client.name for another column. I'm trying to do all of this in one method and I've got a working method in TypeScript.
This is the sortBy method in my component's class:
sortBy(option: SortOption, sortedBy: string) {
const props = option.split('.');
const parent = props[0];
const child = props[1];
const asc = this[sortedBy];
if(!child) {
this.associates.sort((associateA, associateB)=> {
if (associateA[parent] < associateB[parent]) {
return asc === true ? -1 : 1;
} else if (associateB[parent] < associateA[parent]) {
return asc === true ? 1 : -1;
} else {
return 0;
}
});
} else {
this.associates.sort((associateA, associateB)=> {
if (associateA[parent][child] < associateB[parent][child]) {
return asc === true ? -1 : 1;
} else if (associateB[parent][child] < associateA[parent][child]) {
return asc === true ? 1 : -1;
} else {
return 0;
}
});
}
this[sortedBy] = !this[sortedBy]
}
SortOption is an enum, and the value may or may not have a . in it. I split on that period to get the parent and child properties, and then sort the array based on whether the parent or the child exists.
Now what I'm wondering is if there's a better, drier, way to do this. You'll notice the code in both the if and the else statement are almost exactly the same, with the exception of the child property being used or not, but I can't think of a cleaner way to do this.
Is there a better way?
You might want to extract a sorting key function:
let sortingKey: (associate: any) => number; // or some other type, depends on your code
if (child) {
sortingKey = (associate) => associate[parent][child];
} else {
sortingKey = (associate) => associate[parent];
}
After that you will be able to simply write:
this.associates.sort((associateA, associateB)=> {
if (sortingKey(associateA) < sortingKey(associateB)) {
return asc === true ? -1 : 1;
} else if (sortingKey(associateB) < sortingKey(associateA)) {
return asc === true ? 1 : -1;
} else {
return 0;
}
});
I need some help. I have 2 loops to traverse two lists of records . The thing is that at some point should put a break or something, because the last loop crushes the value I'm looking for.
This is my code
for (var i in $scope.users) {
for (var j in $scope.states) {
if ($scope.users[i].id === $scope.states[j].user) {
if ($scope.states[j].estado === 'totallyBussy') {
$scope.users[i].estado= 'Not available';
} else if ($scope.states[j].estado === 'partlyBussy') {
$scope.users[i].estado= 'Maybe available';
}
}
else {
$scope.users[i].estado= 'Available';
}
}
}
Where user 4 and user 5 should be 'Maybe available' and 'Not available', but with this code, I'm getting 'Available for user 0, 1, 2, 3, and the last one. And the last one is crushes with else.
I hope I explained well.
Thanks so much
You can use the break keyword, which will end the loop when executed.
One side note: you don't want to use for (var i in array) {} since you may iterate through Array properties (and have i take unexpected values).
I updated your code with a more reliable iteration.
for (var i = 0, il = $scope.users.length; i < il; i++) {
var user = $scope.users[i];
for (var j = 0, jl = $scope.states.length; j < jl; j ++) {
var state = $scope.states[j];
if (user.id === state.user) {
if (state.estado === 'totallyBussy') {
user.estado = 'Not available';
} else if (state.estado === 'partlyBussy') {
user.estado = 'Maybe available';
}
// Go to the next user (break the "states" loop)
break;
} else {
user.estado = 'Available';
}
}
}
EDIT: If $scope.users and $scope.states are not Arrays but actually Objects (which would seem strange to me but anyway), you can keep for (var i in object) {} but you have to add a check: if (object.hasOwnProperty(i)) {} in the loop.
you can add a flag which will become true when you got your desired value.
something like
var gotValue = false;
for (var i in $scope.users) {
if(!gotValue)
{
for (var j in $scope.states) {
if ($scope.users[i].id === $scope.states[j].user) {
if ($scope.states[j].estado === 'totallyBussy') {
$scope.users[i].estado= 'Not available';
gotValue = true;
} else if ($scope.states[j].estado === 'partlyBussy') {
$scope.users[i].estado= 'Maybe available';
gotValue = true;
}
}
else {
$scope.users[i].estado= 'Available';
gotValue = true;
}
}
}
}
In AngularJS, there is no break for loops,etc., so one has to go with logic manually.
A similar case has been demonstrated here.
Here, I have created a variable f, which has been set to false initially. On forEach loop, based on a condition, it is made to true, further which it works as break.
Please find the code below:
HTML:
<div ng-app="app" ng-controller="test"></div>
JS:
var app = angular.module('app',[]);
app.controller ('test', function ($scope){
var f = false;
angular.forEach([1,2,3,4,5], function(v,k){
if(k > 2){
f = true;
}
if(!f) {
document.write('value = ' + v + ' <br>');
}
});
});
im using ag-Grid, but there is a issue when it filters my data, when i filter my data in the price column, it only works with numbers dot and not with commas.
Link: https://plnkr.co/edit/LDdrRbANSalvb4Iwh5mp?p=preview
Practical Example:
In the Price column select box equal and above insert "1.5" and than try inserting "1,5"
This is because this filter is a native one.
If you want to handle custom behaviour, define your own filter.
Documentation : https://www.ag-grid.com/angular-grid-filtering/index.php
A quick and dirty solution would be to monkey patch the NumberFilter like this :
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
valueAsNumber = value;
}
else {
valueAsNumber = parseFloat(value.replace(',','.'));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Then changed line is here :
valueAsNumber = parseFloat(value.replace(',','.'));
So i found the problem, first i had to convert the value has a string than i needed to replace the dot by the comma, the problem with the answer above was first because of the data type and than the order of the properties of the replace function, but the problem now is that is not filtering correctly, if i search using equal option if gives me 2 values, instead a fixed one, code looks something like this:
Code:
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
value = value.toString()
valueAsNumber = parseFloat(value.replace('.',','));
}
else {
valueAsNumber = parseFloat(value.replace('.',','));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Possible Solution:
NumberFilter.prototype.onFilterChanged = function () {
var filterText = utils_1.default.makeNull(this.eFilterTextField.value);
if (filterText && filterText.trim() === '') {
filterText = null;
}
var newFilter;
if (filterText !== null && filterText !== undefined) {
console.log(filterText);
// replace comma by dot
newFilter = parseFloat(filterText.replace(/,/g, '.'));
console.log(newFilter);
}
else {
newFilter = null;
}
if (this.filterNumber !== newFilter) {
this.filterNumber = newFilter;
this.filterChanged();
}
};
In the codebase that I am trying to figure out, I see that a js file (myloopfile.js) being imported into another js file. I am trying to make sense of some of the code used there
this is myloopfile.js
function method1(value) {
// return something
}
var myLooper = function (obj, iterator, context) {
var key;
if (obj) {
if (typeof obj === 'function') {
for (key in obj) {
if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key);
}
}
} else if (obj.forEach && obj.forEach !== forEach) {
obj.forEach(iterator, context);
} else if (isArrayLike(obj)) {
for (key = 0; key < obj.length; key++)
iterator.call(context, obj[key], key);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key);
}
}
}
}
return obj;
};
……………………………………………………………………………….
the myLoop in myloopfile.js is called like this
var looper = require(‘../myloopfile.js);
looper({
loop1: function(Home) { //do something },
loop2: function(Home) { //dosomething }
}, function(return1, return2) {
//do something else
});
I am trying to find out where this
function(return1, return2) {
//do something else
});
coming from ? I don’t see anything in that file that suggests that there is a method attached to it. Also where are the parameters return1 and return2 coming from? is this some javascript way to attach things ?
var myLooper = function (obj, iterator, context) {
/* .... */
iterator.call(context, obj[key], key);
/* .... */
};
You pass:
looper({
loop1: function(Home) { //do something },
loop2: function(Home) { //dosomething }
}, function(return1, return2) {
//do something else
});
So
obj = {
loop1: function(Home) { //do something },
loop2: function(Home) { //dosomething }
}
and
iterator = function(return1, return2) {
//do something else
}
The Function.prototype.call() method calls a function with a given this value and arguments provided individually. Therefore, inside you iterator function:
this = context;
return1 = obj[key];
return2 = key;
So javascript has function that are called anonymous function that don't need a function name.
Basically it is used (in this instance) as a way to be an expantion of a parameter.
Take for example the javascript function setTimeout
Well setTimeout can take an anonymous function as one of its parameters i.e
var timer = setTimeout(function{
//do something
},
2000);
// setTimeout(function, time, paramters)
So you don't have to declare a function and pass it in as a parameter
Back to your case, you have this anonymous function that takes return1 and return2
So in the end:
return1 = obj[key];
return2 = key;