Arrange NSDictionary unique value in iphone - iphone

Hi i am getting response form Json like this
{"items":[{"f":"djay","m":"hi ajay","d":"2012-9-22","id":"16"},{"f":"djay","m":"hi","d":"2012-9-22","id":"25"},{"f":"Chithri","m":"hi","d":"2012-9-23","id":"26"},{"f":"ChithriAtchibabu","m":"gig","d":"2012-9-23","id":"27"}]}
i want arrange my array like this
[{"time":"2012-9-22","data":[{"m":"hiajay"},{"m":hi}]},{"time":"2012-9-23","data":[{"m":"hi"},{"m":gig}]}]
can any one please guide me how could i arrange my array like this

An NSDictionary is an unsorted collection.
To sort the data in it makes no sense.
What you are doing in your post is completely rebuilding an array of dictionaries from another dictionary.
Also, a dictionary can't have duplicate keys.
To do this you'd have to go through the array "items" in your first dictionary and then create a new dictionary for each new date and then inside there put an array with the items in it. Or something...

Related

add string to an array in Firestore - flutter

i have created an array of strings "challengeMember" in Firestore database and i want to add data to it when user clicks a button, my database:
in my code am trying to update the array, every time user join a challenge, the challenge name will be added to this array i know how to access the array using:
FirebaseFirestore.instance.collection("Users").doc(user!.uid).update({'challengeMember': widget._challengeName});
but the problem with this it is not specifying the array index and it is not checking whether the index is empty or not, so how can i dynamically access this array and keep adding values.
Firestore has a special trick to add an array, and it goes like this:
await FirebaseFirestore.instance.collection('users').doc(uid).update({
'tokens': FieldValue.arrayUnion([token]),
});
See the FieldValue documentation on how to manipulate arrays.

How to increment individual element in an array in firestore for flutter

This is how my data look like.
I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!
I tried using the following code:
firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
.update({
'rating.0': FieldValue.increment(1),
});
But the whole rating became empty as seen
You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.

PostgreSQL: array in text to array

I have JSON array in text type field in DB, and I want to check whether there are some elements in the array, and in that case, add some elements to this array and save it back to DB. The main issue I am having is that this text field looks like this:
["elem1","elem2","elem3"]
and I cannot figure out how to work with those double-quotes.
When I tried to_json it resulted in:
"[\"elem1\",\"elem2\",\"elem3\"]"
When I tried string_to_array:
{"[\"elem1\"","\"elem2\"","\"elem3\"]"}
I just need something like that:
['elem1', 'elem2', 'elem3']
To get a JSON array, simply cast it into json directly:
demo:db<>fiddle
SELECT '["elem1","elem2","elem3"]'::json
If you need a simple array (without JSON), you need to expand the elements of the JSON array (json_array_elements_text()) and aggregate them afterwards (array_agg()):
SELECT
array_agg(elems)
FROM mytable,
json_array_elements_text(mydata::json) AS elems

Query on array from another array

I have the following 2 arrays. Array A with i.e. 10 id's, and the other array, Array B with 300 id's with all the corresponding data.
I want to retrieve all the data from B with id's which are stated in the array A.
I could just loop all entries in array B on every entry in array A, but this looks a little bit heavy for such simple task. What would be the best solutions in this case?
Thnx!
If you don't need an ordered array you could switch to an hash table (eg. NSSet) which has a much faster lookup time (strong bias towards O(1)). Otherwise you had to loop through the whole array and check all of its members.
Probably use a predicate (a, b are your arrays):
NSPredicate* filter = [NSPredicate predicateWithFormat: #"self IN %#", a];
NSArray* result = [b filteredArrayUsingPredicate: filter];
Although, it might only work if the objects in your arrays are simple things like strings.
When you are filling array B you can instead make it a dictionary. Assuming ids are unique you can then get value of the object for key in array A from array B. This will avoid looping and is sort of equivalent to hash table.
The returned value will then have the corresponding data for the particular unique id.
I don't see how you are going to do it otherwise if you are using arrays. The only way to access the right element is by looping through the array.

Date wise sectioning of a UITableView

Suppose I have a UITableViewController with an array "history" as data source. The array consists of an undefined number of NSDictionaries. Each dictionary contains the following keys: term (NSString), date (NSDate) and id (NSNumber).
I would like to map the contents of the history array according to the date values each dictionary has, i.e. I need to dynamically create sections, named and sorted according to the date values of each dictionary (the iPhone app wikipanion does this in its history view). Is there a best practice to do this? Otherwise I would just read out each date object, sort them into another dictionary, etc...
Best practice is to 'group by YYYY-MM-DD' and then 'sort by YYYY-MM-DD HH:MM:SS'. So this involves some simple grouping of your dictionaries into arrays. Now that you have an array of arrays you can sort those simply by date.