I've tried:
if(angular.isUndefined(value)){
// something
}
and
if(!(value)){
// something
}
Is there a difference between the two?
Is there a use-case to choose one instead of the other?
var foo = false;
if(!foo) {
// will log
console.log("foo is defined but false");
}
if(angular.isUndefined(foo)){
// will not log as foo is defined
console.log("foo is undefined")
}
another example without define foo
if(!foo) {
// will throw exception "Uncaught ReferenceError: foo is not defined "
console.log("foo is defined but false");
}
if(angular.isUndefined(foo)){
// will log
console.log("foo is undefined")
}
so effective angular.isUndefined(foo) does nothing else than evaluating
if(typeof foo == "undefined")
wrapped for saving 1 character yeah.
while !-operator checks if a defined variable evaluates to false
so
if(!foo)
is the same like
if( foo != true)
UPDATE:
As stated in comments, when i write "evaluates to false" there is false null undefined NaN ""(empty string) and 0 included
! is the logical not operator in JavaScript while angular.isUndefined(value) checks if the reference is undefined
Which one to use completely depends on what you are trying to do in the end.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators and https://docs.angularjs.org/api/ng/function/angular.isUndefined
Related
How do I write in a loop instead of a ternary operator ?
temp->status = (inStore ? waiting : called);
would it be like:
if (inStore){
return waiting;
}
else (
return called;
}
I'm unsure becauseI get an error doing this, I'm using it in a void function
The problem is here: else (. Change the ( to { and your compile should be clean.
The ternary operator is simply an if-then-else statement in a shortcut syntax, with the assignment statement presented as its single lvalue. So:
temp->status = (inStore ? waiting : called);
is translated to:
if(inStore == true)
{
temp->status = waiting;
}
else
{
temp->status = called;
}
Note that there is nothing wrong with your syntax (except maybe the "(" here: else (). In a function, it might be preferable to use return statements if the function required no clean up before leaving.
You have to assign waiting or called to temp->status variable instead of returning it, also in else you used parenthesis wrongly. Whole thing should be:
if (inStore)
temp->status = waiting;
else
temp->status = called;
I am not sure why you asked for a loop in that case, as there is no need to use one.
if(inStore){
temp->status = waiting;
} else { // there was a ( instead of a {
temp->status = called;
}
I am using a if condition in angularjs to check is a status is != Delivered or status is != Canceled then alert a message
if($scope.itemDeliveredStatus != "Canceled" || $scop.itemDeliveredStatus != "Delivered"))
{
alert("Please set item status to Delivered! or Canceled!");
return false;
}
Typo on the second condition, you put $scop instead of $scope :
$scop.itemDeliveredStatus
and an extra ) at the end of that line which is not necessary
First you do have a typo, but that is only one small issue. The other is a logic problem.
Your logic is wrong with the use of OR. Simple example below shows you that.
var foo = "A";
if (foo !== "A" || foo !== "B") {
console.log("WHY?")
}
Why? Because you are saying that in order for this if statement to be true, the variable foo has to be equal to two things.
You want it to say if foo does not equal A AND foo does not equal B, than show error.
var foo = "A";
if (foo !== "A" && foo !== "B") {
console.log("WHY?")
} else {
console.log("Better!!!")
}
So with the two issues fixed, your code would look like:
if ($scope.itemDeliveredStatus != "Canceled" && $scope.itemDeliveredStatus != "Delivered") { /*.. code */ }
I have a annidate variable like that.
$scope.a = { b: {c : 1} };
var test = $scope.a.b.c;
// test == 1
$scope.a = {}
var test = $scope.a.b.c;
// ERROR
I want test variable will be null or undefined.
How can I fill test variable without error?
some advice?
I'm looking for a smart way
not only
if(angular.isDefinied($scope.a) && angular.isDefinied($scope.a.b) && angular.isDefinied($scope.a.b.c))
You probably want to do something like that:
var test = ($scope.a && $scope.a.b && $scope.a.b.c) ? $scope.a.b.c : undefined;
If you are not defined the value before using it, then definitely, it will throw an error. You must define or you must check whether it is defined or not. Otherwise you can't resolve the error. var test will get a value only if there is no error. Now, its up to you to decide and use a smart way
var test = $scope.a?((typeof($scope.a.b)=="object" && $scope.a.b.c)?$scope.a.b.c:(typeof($scope.a)=="object" && $scope.a.b)?$scope.a.b:null):null
how can i check if any javaScript object's property exists and if it exists then it has a valid value?
actually,i am a beginner and trying to solve this-
Check if the second argument is truthy on all objects of the first argument(which is an array of objects).i.e.
check if the second argument exists in all the objects in first argument(an array) as a property.
if it exists, it should not be-
invalid, as age can't be 0
null
undefined
empty string('')
NaN
till now i have this-
function truthCheck(collection, pre) {
for(var i=0;i<collection.length;i++){
if(!(pre in collection[i])||collection[i]
[pre]===undefined||isNaN(collection[i]
[pre])||collection[i][pre]===""||
collection[i][pre]===null||collection[i]
[pre]===0)
{
return false;
}
}
return true;
}
i know this is not the best wayto solve .Is there a better way to do this?i don't like that long if statement in my code.i have seen other SO links-link1,link2 but none of them seemed to solve my query. any kind of help is highly appreciated.
P.S. this code is not working for some true cases even.
o = new Object();
o.prop = 'exist';
if(o.hasOwnProperty('prop')){
if(o['prop']){
alert('good value')
}
}
https://stackoverflow.com/a/6003920/1074179
this is what i was looking for and absolutely logical-
for(var i in array){
if((prop in array[i])&& Boolean(array[i][prop]))
{
//do something
}
}
the Boolean() function is something which made my day. Learn more at this link.
Look at the below example.
let the json object be
var a = { obj1:"a",obj2:"b"}
to check if an object exists,you can use hasOwnProperty() method.
a.hasOwnProperty("obj2") //this will return true
a.hasOwnProperty("obj3") // this will return false
to check the value of an object
if(a["obj1"] && a["obj1"]!="" && a["obj"]!=0){
//place your logic here
}
I have the following code:
public fun findSomeLikeThis(): ArrayList<T>? {
val result = Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>
if (result == null) return null
return ArrayList(result)
}
If I call this like:
var list : ArrayList<Person>? = p1.findSomeLikeThis()
for (p2 in list) {
p2.delete()
p2.commit()
}
It would give me the error:
For-loop range must have an 'iterator()' method
Am I missing something here?
Your ArrayList is of nullable type. So, you have to resolve this. There are several options:
for (p2 in list.orEmpty()) { ... }
or
list?.let {
for (p2 in it) {
}
}
or you can just return an empty list
public fun findSomeLikeThis(): List<T> //Do you need mutable ArrayList here?
= (Db4o.objectContainer()!!.queryByExample<T>(this as T) as Collection<T>)?.toList().orEmpty()
try
for(p2 in 0 until list.count()) {
...
...
}
I also face this problem when I loop on some thing it is not an array.
Example
fun maximum(prices: Array<Int>){
val sortedPrices = prices.sort()
for(price in sortedPrices){ // it will display for-loop range must have iterator here (because `prices.sort` don't return Unit not Array)
}
}
This is different case to this question but hope it help
This can also happen in Android when you read from shared preferences and are getting a (potentially) nullable iterable object back like StringSet. Even when you provide a default, the compiler is not able to determine that the returned value will never actually be null. The only way I've found around this is by asserting that the returned expression is not null using !! operator, like this:
val prefs = PreferenceManager.getDefaultSharedPreferences(appContext)
val searches = prefs.getStringSet("saved_searches", setOf())!!
for (search in searches){
...
}