Which is better way to compare the definition of an object ?
if(obj!==null & angular.isDefined(obj)) or if(obj) ?
Would love to know the use and abuse of both methods.
Thank you in advance.
obj = false;// this will work with obj=0; too
if(obj!==null & angular.isDefined(obj)) {
// GOING IN
}
if(obj){
// NOT GOING IN
}
So : if you have no idea of the type of data you're testing and you need 0 and false to get in, use the angular.isDefined along with null check. Otherwise if you know what you're doing you may use if(obj) {} if it suits.
Related
I have an object of $person as below:
$person = Person::where('id', $id)->first();
According to which $person exists or not I load other data:
if($person) {
$person->family_members = FamilyMemberController::FamilyMemberOf($person->id);
} else {
$person->family_members = [];
}
In the view file, I check the $person->family_members if not empty and exists to add a generated value :
if(!empty(array_filter($person->family_members))) {
// my code
}
But it throws an error:
array_filter(): Argument #1 ($array) must be of type array, Illuminate\Database\Eloquent\Collection given
I need to check this $person->family_members to make sure whether it's an array or a collection is not empty.
Writing code for if array do something if collection do something is the wrong way of implementation.
You can do two things.
use both returns as collection()
or either use both returns as an array[]
If collection
else {
$person->family_members = collect();
}
If array
use ->toArray() at the end of Eloquent. Check this answer
As well, I think you are confused with array_filter(). Maybe you are searching for in_array() or contains()
Use count method
if(count($person->family_members)>0){
//your code
}
We don't know your code, but given the error, it's safe to assume your method returns a Collection. You can see available methods in the docs. However, if you need it as array, all you need to do is call ->toArray() on the result Collection. Then you can use array_filter.
What about just doing
if(!$person->family_members){
// your code here
}
or
if($person->family_members){
// your code here
} else {
// code of "if it's empty" goes here
}
You can use the count() function to return a count of an index. ex
if(count($person->family_members)){
//do your true algo
}
Why you are using the empty method ?! Try this:
$person->family_members = $person ? FamilyMemberController::FamilyMemberOf($person->id) : null;
if($person->family_members){ // your code }
Try simple ways in Programming ;)
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
}
if (isset($_REQUEST["$something"])) {
$user["$something"] = $_REQUEST["$something"];
}
Need to change the variable name for use in class.
Easy way?
I didn't figure out your question. Maybe do you want pick up the value from resquest and put in your Array ?
if (isset($_REQUEST["something"])) {
$user["something"] = $_REQUEST["something"];
}
As part of a xtype combo, I would like to know if the layer I choose in my simple data store (represented by this.getValue()) is present in the map layers. So if it does, A should occur, and B if it does not. The problem is that myLayer variable seems to be unrecognized, even though Opera Dragonify throws no error at all. Where would be the error?
listeners: {
'select': function(combo, record) {
for(var i = 0; i < mapPanel.map.length; i++) {
var myLayer = mapPanel.map.layers[i].name;
if (myLayer == this.getValue()) {
// do A here...
} else {
// do B here...
}
}
}
}
Thanks for any pointers,
I think the problem is that you are using this.getValue() instead of using combo.getValue().
I don't know how your app is set but it's usually a better idea to use the first parameter of your listener instead of the keywork this in order to avoid scope issues.
Hope this helps
#Guilherme Lopes Thanks for that, but the solution was this: mapPanel.map.layers.length instead of mapPanel.map.length.
The other day, a colleague noticed the common pattern he was doing of:
if (someBooleanFlag)
{
someBooleanFlag = FALSE;
...do some more stuff...
}
basically, a clear-on-read latch. And asked if I knew of a clever way to test the flag and clear it as a one-liner, so he could get rid of the boilerplate someBooleanFlag = FALSE; bit. Questions about whether this is good style or not aside, I found the best I could do for him was a something like
#define TESTANDCLEAR(var) (var ? var-- : 0)
This makes the assumption that ONLY 1 and 0 are being used, and it doesn't work on bitmasks either. I figured I'd turn to the Wizards of the Stack, to see if there was some better way and other technique that could be used.
(Again, no need to discuss whether the style of doing a TESTANDCLEAR() expression is bad or good, it was more of the academic exercise if we actually could do it, and how generally)
I don't think you need to make the 0/1 assumption, even with the approach you already have. Assignments are expressions that return a value, so you could define that macro as:
(var ? !(var = false) : false)
Or, you could use the comma operator:
(var ? ((var = false), true) : false)
Which would help if you wanted this to work with a bitfield testing for the Nth bit:
(((var & (1 << N)) ? ((var = var & ~(1 << N)), true) : false)
Ugly, but I think this should work:
if (someBooleanFlag && !(someBooleanFlag = !someBooleanFlag))
For the love of God never do this in production code.