I'm trying to filter a list of users from a Firebase query so that I only get users that are NOT listed in one of two arrays. The code I'm using doesn't work:
let users = snapshot.childSnapshots.map {
User(snapshot: $0)
}.filter{
guardiansArray.contains($0.key) == false || dependentsArray.contains($0.key) == false
}
If I remove the ==false code, I get the opposite effect of what I want: I get a list of users that ARE in either of the two arrays. How can I get the reverse effect?
Thanks!
It looks like you want to use && instead of ||.
Related
I am trying to filter some information with 2 conditionals (something is true and something else is >0)
Independently things work just fine:
=unique(filter(
indirect($A$1&"!$E$3:E"),
indirect($A$1&"!$C$3:C")=TRUE
))
gives me a list of things that are true, and
=unique(filter(
indirect($A$1&"!$E$3:E"),
indirect($A$1&"!$J$3:J")>0
))
gives me a list of things that are >0.
When i try to combine them, like this
=unique(filter(
indirect($A$1&"!$E$3:E"),
indirect($A$1&"!$C$3:C")=TRUE,
indirect($A$1&"!$J$3:J")>0
))
I get an error No matches are found in FILTER evaluation.
What am i missing please?
PS: It goes without saying that I do indeed have things that are both true and are > 0
for non-english locale it would be:
=unique(filter(
indirect($A$1&"!$E$3:E");
indirect($A$1&"!$C$3:C")=TRUE;
indirect($A$1&"!$J$3:J")>0))
I am working with a method which filters the preferences which match with the ids, I am using the contains method, but even though the values are the same, the contains is showing a false in each iteration. The method looks like:
private fun filterPreferencesByIds(context: MyPodCastPresenterContext): List<FanPreferences> {
return context.preferences?.filter {
context.ids.contains(it.id)
}
}
The values of the arrays are:
for the context.ids:
"B52594F5-80A4-4B18-B5E2-8F7B12E92958" and "3998EDE7-F84B-4F02-8E15-65F535080100"
And for the context.preferences:
But even though, when the first and the final ids have the same id value as the context.ids, the contains is false in the debug. I think it could be related with the types in the context.ids rows Json$JsonTextNode. Because when I did the same with numeric values hardcoded the compare is successful.
Any ideas?
Thanks!
If the type of FanPreferences.id is String and the type of context.ids list element is JsonTextNode, you won't find an element equal to the given id string, because it's not of String type.
Try mapping your context ids to the list of strings before filtering:
val ids = context.ids.map { it.toString() }.toSet()
return context.preferences?.filter {
ids.contains(it.id)
}
Note that calling toString() on JsonTextNode might be not the best way to get the string data from it. It's better to consult the API documentation of that class to find it out.
How to use multiple setQuery on solr?
i want to search name="din" and staus="active" and email="din"
am using like this
if(isset($_GET["name"]) && $_GET["name"]!=""){
$query->setQuery('name:*'.$_GET["name"].'*');
}
if(isset($_GET["email"]) && $_GET["email"]!=""){
$query->setQuery('email:*'.$_GET["email"].'*');
}
if(isset($_GET["role"]) && $_GET["role"]!=""){
$query->setQuery('role:*'.$_GET["role"].'*');
}
if(isset($_GET["status"]) && $_GET["status"]!=""){
$query->setQuery('status:'.$_GET["status"]);
}
But its not giving proper response
let me know how to use ?
->setQuery() sets the query. If you want to set several conditions, you'll have to provide all of them. Also remember that any $_GET variable can contain spaces and similar values, and should be properly escaped.
You can build a query by keeping each term by itself, before merging it into a single query string at the end:
$queries = array();
if (!empty(if($_GET["name"])) {
// add proper escaping here, so you don't escape * as well.
$queries[] = 'name:*'.$_GET["name"].'*';
}
// you probably want to check that there actually is any queries here as well
$query->setQuery(join(" AND ", $queries));
So, for sending to individual streams we have to reference the connected netStream we want to send to in some way like this:
sendStream.peerStreams[0].send("MyFunction",param1,param2);
and I have to determine which peer I'm sending to by their ID such as "peerID1234"
I know that you can check the peerID of the stream by doing:
sendStream.peerStreams[0]["farID"]
how can I make my send stream function know to use the array index where the peerID is?
so basically it could be like:
sendStream.peerStreams[where peerStreams[]["farID"] == peerID].send("MyFunction",param1,param2);
Sounds like you'll have to loop through the peerStreams array to find the object that has the right farID property value. Basically you are searching through the array for an item with a specific property value. There is no built-in functionality for this. But you can do it with a simple loop. Something like this:
var correctStream:Object = null;
for each (var stream:Object in sendStream.peerStreams) {
if (stream["farId"] == peerId) {
correctStream = stream;
break;
}
}
correctStream.send("MyFunction",param1,param2);
Note that I don't know what the data type is for the peerStreams object so I just typed it as Object in my example.
There's some other approaches mentioned here but they are just different styles of doing the same thing.
How do I figure out if an array contains an element?
I thought there might be something like [1, 2, 3].includes(1) which would evaluate as true.
Some syntax sugar
1 in [1,2,3]
.contains() is the best method for lists, but for maps you will need to use .containsKey() or .containsValue()
[a:1,b:2,c:3].containsValue(3)
[a:1,b:2,c:3].containsKey('a')
For lists, use contains:
[1,2,3].contains(1) == true
If you really want your includes method on an ArrayList, just add it:
ArrayList.metaClass.includes = { i -> i in delegate }
You can use Membership operator:
def list = ['Grace','Rob','Emmy']
assert ('Emmy' in list)
Membership operator Groovy
IMPORTANT Gotcha for using .contains() on a Collection of Objects, such as Domains. If the Domain declaration contains a EqualsAndHashCode, or some other equals() implementation to determine if those Ojbects are equal, and you've set it like this...
import groovy.transform.EqualsAndHashCode
#EqualsAndHashCode(includes = "settingNameId, value")
then the .contains(myObjectToCompareTo) will evaluate the data in myObjectToCompareTo with the data for each Object instance in the Collection. So, if your equals method isn't up to snuff, as mine was not, you might see unexpected results.
def fruitBag = ["orange","banana","coconut"]
def fruit = fruitBag.collect{item -> item.contains('n')}
I did it like this so it works if someone is looking for it.
You can also use matches with regular expression like this:
boolean bool = List.matches("(?i).*SOME STRING HERE.*")