firebase get key value and store it in array - firebase

I build ionic 3 app and I'm using firebase.
I have a structure of details that I want to get the key and value and store it in array.
some questions:
which kind of array I can do in typescript?
2.I want to get the keys and the values of this structure:
"Years": [
1:"mechina",
2:"Year A",
3:"Year B",
4:"Year C",
5:"Year D"
],
the numbers are the key. what I tried , it give me only the values but i need to key also
this.collegeProvider.loadYears().on('value',years =>{
this.yearsArray = years.val();
console.log(this.yearsArray);
});

I am not sure of the json structure you have provided but the firebase json tree looks something like this,
{
"-KsdJ5ngvltq1eOQJ6JS" : {
"a" : "",
"b" : "",
"c" : "",
"d" : "",
"e" : "",
"f" : "",
"g" : "kaushalagarwal79#gmail.com",
"h" : "",
"i" : "",
"j" : "",
"k" : ""
},
So while pushing your data in firebase you can code something like,
myRef.push().setValue(new book(n, a, cn, cc, r, c, e, p, d, ed, pub, iE));
key = myRef.push().getKey();
So in this case the key received is, -KsdJ5ngvltq1eOQJ6JS
U can create a array for numerous objects,
Hope that helps!!!

Related

How to store a map using :dets in Elixir?

I want to be able to store a map using :dets
Currently, that is the solution I am trying to implement:
# a list of strings
topics = GenServer.call(MessageBroker.TopicsProvider, {:get_topics})
# a map with each element of the list as key and an empty list as value
topics_map =
topics
|> Enum.chunk_every(1)
|> Map.new(fn [k] -> {k, []} end)
{:ok, table} = :dets.open_file(:messages, type: :set)
# trying to store the map
:dets.insert(table, [topics_map])
:dets.close(table)
However, I get
** (EXIT) an exception was raised:
** (ArgumentError) argument error
(stdlib 3.12) dets.erl:1259: :dets.insert(:messages, [%{"tweet" => [], "user" => []}])
How is it possible to accomplish this?
I have tested by erlang. You should convert the map to list first.
Following from dets:insert_new() doc
insert_new(Name, Objects) -> boolean() | {error, Reason}
Types
Name = tab_name()
Objects = object() | [object()]
Reason = term()
Inserts one or more objects into table Name. If there already exists some object with a key matching the key of any of the specified objects, the table is not updated and false is returned. Otherwise the objects are inserted and true returned.
test code
dets:open_file(dets_a,[{file,"/tmp/aab"}]).
Map = #{a => 2, b => 3, c=> 4, "a" => 1, "b" => 2, "c" => 4}.
List_a = maps:to_list(Map). %% <----- this line
dets:insert(dets_a,List_a).
Chen Yu's solution is good, but before getting it I already found another solution.
Basically, you can just add the map to a tuple
:dets.insert(table, {:map, topics_map})
Then, you can get this map by using
:dets.lookup(table, :map)
As I understood your intent, you want to store users and tweets under separate keys. For that, you need to construct a keyword list, not a map, in the first place.
topics = for topic <- topics, do: {topic, []}
# or topics = Enum.map(topics, &{&1, []})
# or topics = Enum.map(topics, fn topic -> {topic, []} end)
then you might use this keyword list to create dets.
{:ok, table} = :dets.open_file(:messages, type: :set)
:dets.insert(table, topics)
:dets.close(table)

Kotlin standard approach of making new map from an immutable map and additional key-value

Is there a standard way in Kotlin to associate a new key-value pair with an immutable map?
associate(mapOf("A" to 1, "B" to 2), "C", 3); // => {A=1, B=2, C=3}
Thinking about something similar to accos function in Clojure.
(assoc {:key1 "value1" :key2 "value2"} :key3 "value3")
;;=> { :key1 "value1", :key2 "value2", :key3 "value3"}
It is obvious how to make it directly copying entries into a new map, but I believe there is more optimal approach way implemented in the Kotlin standard library. Could you give an example of making this idiomatically?
This is done using the plus operator.
val map1 = mapOf("A" to 1, "B" to 2)
val map2 = map1 + ("C" to 3)
// or for a read-write variable/property:
var map = mapOf("A" to 1, "B" to 2)
map += "C" to 3
If you want to add more than one item at once, you can put any collection or array of pairs after the plus sign:
val map1 = mapOf("A" to 1, "B" to 2)
val map2 = map1 + arrayOf("C" to 3, "D" to 4)
Note that the correct terminology for a Map is “read-only” map, not “immutable” map. A Map is often a MutableMap under the hood that has been cast to a read-only Map, so it is not actually immutable. There is a common pattern in Kotlin of a class exposing access to a mutable collection through a property that upcasts it to a read-only implementation but continues to mutate that reference for the outside classes to observe changes.

How does Firestore query operation strings treat different data types?

How does the Firestore opStr (Operation Strings) in the where query method treat different data types?
The different operation strings are: <, <=, ==, >, and >=
For number data type it is very self explanatory, but how about for the other data types? string, boolean, object, array, null, timestamp, geopoint, and reference
So for e.g. string data type, does >= mean is equal to or contains string?
So db.collection('users').where('lastname','>=','bar') would return all users that has a lastname of bar or contains bar? e.g. bar, foobar, barbaz
Does anyone know of any documentation for this specific subject?
For string types, the >= evaluates based on the lexicographical ordering of the values.
Some examples:
"a" < "b"
"aaa" < "aab"
"abc" < "abcd"
And also:
"000" < "001"
"010" < "011"
"100" < "101"
But for example:
"2" > "10"
Since the "2" is alphabetically later than the "1" in unicode.

hjson: why does close brace have to be on a separate line?

This works: (update: but not as I was thinking! it actually sets b = "c, d: e")
a: [
{ b: c, d: e
}
]
and this works:
a: [
{ "b": "c", "d": "e" }
]
But this doesn't work. What about the hjson definition disallows the closing brace at the end of the line?
a: [
{ b: c, d: e }
]
Found ']' where a key name was expected
(check your syntax or use quotes if the key name
includes {}[],: or whitespace): line 3 column 1 (char 23)
In Hjson a string without quotes is terminated by the newline, so your closing brace gets eaten by the quoteless string.
When you write
{ b: c, d: e
}
you are saying, give me a string that contains "c, d: e".
You need to use either quotes
{ b: "c", d: "e" }
or
{
b: c
d: e
}

asp.net array / arraylist error

I'm working on something relatively simple (or so I thought) and need a bit of help.
I am trying to create a dynamic amount of comma separated strings.
I have a variable (numberOfStrings) which is the number of different strings I need.
I just want to loop thru the aryDrivers and assign then to the different strings.
Dim aryHeats(numberOfStrings - 1) As ArrayList
Dim aryDrivers() As String
aryDrivers = txtBatch.Text.Split(",")
For i As Integer = 0 To aryDrivers.Length - 1
For j As Integer = 0 To aryHeats.Length - 1
aryHeats(j).Add(aryDrivers(i) & ",")
Next
Next
For some reason I'm getting an error in the loop when I try to "ADD" the string.
Thoughts?
Thanks!
** Update **
Maybe this will help explain more what I'm trying to do.
I have a string:
s = A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
i'm passing a variable (numberOfHeats) lets use 4.
i would like to then have 4 strings (so I am wanted to use array)
ary(0) = A,E,I,M,Q,U,Y
ary(1) = B,F,J,N,R,V,Z
ary(2) = C,G,K,O,S,W
ary(3) = D,H,L,P,T,X
hopefully that clears this up.
You could use LINQ, although i hate VB.NET method syntax:
Dim text = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
Dim numberOfHeats = 4
Dim aryHeats As String()() = s.Split(","c).
Select(Function(w, index) New With {.Word = w, .Index = index}).
GroupBy(Function(x) x.Index Mod numberOfHeats).
Select(Function(grp) grp.Select(Function(x) x.Word).ToArray()).
ToArray()
Explanation: it takes the initial string and split it in words (comma as separator). Then it transforms the word and the according index in the String-Array to an anonymous type with Word and Index as properties. This list will be grouped by Index Mod numberOfHeats(the number of arrays you want). This implicitely orders by your desired result. The last step is to transform the groups to a jagged array.
Result:
(0)
(0) "A"
(1) "E"
(2) "I"
(3) "M"
(4) "Q"
(5) "U"
(6) "Y"
(1)
(0) "B"
(1) "F"
(2) "J"
(3) "N"
(4) "R"
(5) "V"
(6) "Z"
(2)
(0) "C"
(1) "G"
(2) "K"
(3) "O"
(4) "S"
(5) "W"
(3)
(0) "D"
(1) "H"
(2) "L"
(3) "P"
(4) "T"
(5) "X"
Try this
Dim aryHeats(numberOfStrings - 1) As ArrayList
Dim aryDrivers() As String
aryDrivers = txtBatch.Text.Split(",")
For i As Integer = 0 To aryDrivers.Length - 1
aryHeats( i Mod aryHeats.Length ).Add(aryDrivers(i) & ",")
Next

Resources