React Native - Firebase Realtime Database and storing arrays - arrays

Say I have an array of strings: var myFavouriteSites = ["www.fb.com", "www.xe.com", "www.youtu.be"];
Is there a way I can push this array into the realtime database as one 'field'? All stored in one place? Because I have read that they must be matched with a unique id/key and I am not familiar of how to do it.

I'm not sure what you mean by "in one place". But I can tell you that everything in Realtime Database is essentially key/value pairs, where the values are either strings, numbers, or other objects. There is no native "array" value type. Arrays get translated into objects where the keys are array indices. So if you assign an array at /location:
["www.fb.com", "www.xe.com", "www.youtu.be"]
You'll get a database structure that looks like this:
/location
0: "www.fb.com"
1: "www.xe.com"
2: "www.youtu.be"

Related

How to store data as an associative array in UiPath

I'm looking for any solutions to keep data by using associative array.
Now i found only way to do is just keep in array. Any for suggestion.
example the result that i want is something like basic concept of associative array
as we know... it had key and value.

What's the difference between an array and a hash whose keys are integers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
What is the difference between an array, an ordered collection of integer-indexed values, and a hash, a collection of key-value pairs, where every key is an index that starts from zero?
arr = [1,2,3,4]
hash = { 0 => 1, 1 => 2, 2 => 3, 3 => 4}
I know that it would be stupid to implement a hash like that, but what is the difference behind the hood? Are they stored in different ways in memory? What data structure makes your program faster when retrieving data from it?
Speed
Arrays are a better for iterating over a collection.
Hashes have almost constant lookup time O(log n), while Arrays have linear time O(n).
This means that you should prefer Hashes when you need to use #include?.
Hashes work great as dictionaries.
There is a third underutilised data structure in Ruby which behaves like a unique Array but uses a Hash underneath. This is Set. It works great when you don't care about the order of elements, need to check if something belongs to a group or need a unique collection of elements.
Implemetation
Yes, they are two completely different data structures.
In this particular example you could access these elements in a similar manner by calling arr[0] and hash[0].
The line arr[0] is in fact sugar syntax for arr.[](0). Same goes for arr[0] = 5 which is the same as arr.[]=(0, 5). Both Array and Hash have methods called [] and []=, but under the hood completely different things would happen, because these are two separate methods with the same name.
Arrays are always indexed with integers starting with 0 and the order of items is important.
Array is a simple list of elements backed by a dynamic C array, while a Hash is an implementation of a dictionary/hashmap, a much more sophisticated data structure.
Hashes store both keys and values, and possess the ability to retrieve a value based on the received key. Every single Ruby object that implements #eql? and #hash can become a Hash key.
The order of elements in a Hash can change and is not stable.
Every Hash is in fact an array of slots called buckets. These buckets hold individual key-value pairs. To determine which key should correspond to a certain bucket, Ruby uses a hashing function, provided by the method called #hash on the object passed as the key. If two objects return the same value from their #hash method, they are considered the same key.
This goes both ways, Ruby uses #hash to save the key-value pair in the right bucket, and to find the right value for the passed key.
For example "string".hash will always return the same value, even though these are different objects.
This value is calculated based on the content of the String. This is why Strings with the same content (even though they are different objects) are considered the same key in a Hash.
Sometimes two different objects (of separate classes) end up having the same #hash. Ruby handles these collisions by implementing individual buckets as linked lists of key-value pairs. So a few key-value pairs may be stored in the same bucket.
To decide which value should be returned for a given key, when there are a few key-value pairs in the same bucket, Ruby utilises the eql? method.
It compares each key stored in the bucket with the passed key like so passed_key.eql?(key_in_bucket). When they are equal Ruby considers them a match.
Here's a great article on Hashes.
Almost every object in Ruby has a hash method. This method calculates some number which is unique-ish. That number is used to retrieve a key in a Hash object. If you want to get the value for 2 then the hash value for 2 is calculated (for an integer this is really fast) and looked up in the Hash object. So it looks like a hash is a way to over-complicate things - but if you want to be sure, benchmark your use-case is the way to go.

MongoDB: Determine index of item in array

Is there a way to determine the index of a retrieved item in an array in MongoDB?
I have an array of object ids inside some document.
{
ids: [id1, id2, id3, ...]
}
I search the array for id3, and if it's found, I also want the position of the object added to the result. In this case, it's so I can insert more objects immediately after it in the array.
If I can't do this, how would I go about creating an ordered list of objects for which I can retrieve and easily modify the position of elements in the ordered list (since an insert operation on the list will result in modifying the position of all elements after the insert)?
answer:
Obtaining the positional index is NOT supported in mongodb. I will open a new question then about redesigning my application to deal with this limitation. Thanks everyone!
The way I see it, you have two choices:
Retrieve the whole array, mutate it however you wish in your language of choice, and update the array in the db with your new array.
Come up with a data structure that supports whatever it is you are ultimately trying to do.

filter an array on the basis of some property of the objects of array

I have an array with some objects stored in it.
Say I have objects of type application.
Each application object has some information associated with it like applicationType, appId etc.
I need to create seperate arrays for each applicationType.
like everytime I need to fetch out the objects of same applicationType.
i.e at the end i need to have an array that will consist of objects ( that will be arrays of same applicationType)
eg : object at 0th index will be an array of application of applicationType1.
object at 1st index will be an array of application of applicationType 2.
etc....
What is the most efficient way to do this ?
You create the array-of-arrays, then iterate over your original array of objects, find out the type of each & add it to the appropriate array in your array-of-arrays, creating a new one each time you encounter a type for the first time. You'll need a mapping from types to indices in your array-of-arrays; a map (or dictionary) would be good for that. In fact, if you have the flexibility, I'd make the array-of-arrays a map/dictionary and cut out the middleman.

Data representation issue on AppEngine Datastore Object

Structure of Data:
2-D string array of column length 3.
Eg. {["One","Two","Three"],["One","Two","Three"],["One","Two","Three"],...}
Datatypes tried & did not work:
String[][3]
ArrayList<String[3]>
How this data be represented , if not by above Datatypes ?
If you're using the com.google.appengine.api.datastore API, entity properties must be of one of the datastore native types, or a Collection of values of a datastore native type (or types). A single property value can't represent a two-dimensional list without serialization, nor can multiple values for a single property name (the Collection case).
If you can serialize, then that's a potential answer. If you have requirements for indexing values in your array for queries, then you'll need to describe those to figure out an answer. For example, entries in the array could be separate entities, each with a "first", "second", and "third" property; the main entity has a List of Keys.

Resources